UPDATE query using PDO

  1. UPDATE query with positional placeholders
  2. UPDATE query with named placeholders
  3. Comments (10)

First of all make sure you've got a properly configured PDO connection variable that needs in order to run SQL queries with PDO and to inform you of the possible errors.

In order to run an UPDATE query with PDO just follow the steps below:

UPDATE query with positional placeholders

As usual, positional placeholders are more concise and easier to use

$sql "UPDATE users SET name=?, surname=?, sex=? WHERE id=?";
$stmt$pdo->prepare($sql);
$stmt->execute([$name$surname$sex$id]);

or you can chain execute() to prepare():

$sql "UPDATE users SET name=?, surname=?, sex=? WHERE id=?";
$pdo->prepare($sql)->execute([$name$surname$sex$id]);

UPDATE query with named placeholders

In case you have a predefined array with values, or prefer named placeholders in general, the code would be

$data = [
    
'name' => $name,
    
'surname' => $surname,
    
'sex' => $sex,
    
'id' => $id,
];
$sql "UPDATE users SET name=:name, surname=:surname, sex=:sex WHERE id=:id";
$stmt$pdo->prepare($sql);
$stmt->execute($data);

or you can chain execute() to prepare():

$sql "UPDATE users SET name=:name, surname=:surname, sex=:sex WHERE id=:id";
$pdo->prepare($sql)->execute($data);

Remember that you should't wrap every query into a try..catch statement, letting a possible error to bubble up to either the built-in PHP error handler or your custom error handler.


Related articles: