SyntaxStudy
Sign Up
PHP Inserting Data with PDO
PHP Intermediate 4 min read

Inserting Data with PDO

PDO INSERT

Use prepare() and execute() to insert rows safely. Retrieve the auto-increment ID with lastInsertId().

Example
<?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;
Pro Tip

Always hash passwords with password_hash() before storing — never store plain text passwords.