PDO INSERT
Use prepare() and execute() to insert rows safely. Retrieve the auto-increment ID with lastInsertId().
Use prepare() and execute() to insert rows safely. Retrieve the auto-increment ID with lastInsertId().
<?php
$stmt = $pdo->prepare(
"INSERT INTO users (name, email, password, created_at)
VALUES (:name, :email, :password, NOW())"
);
$stmt->execute([
"name" => $name,
"email" => $email,
"password" => password_hash($plainPass, PASSWORD_BCRYPT),
]);
$newUserId = $pdo->lastInsertId();
echo "Created user with ID: " . $newUserId;
Always hash passwords with password_hash() before storing — never store plain text passwords.