UPDATE query using PDO

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

Running UPDATE query with PDO is not different from running any other query:

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$dpo->prepare($sql);
$stmt->execute([$name$surname$sex]);

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

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

UPDATE query with named placeholdreds

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$dpo->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";
$dpo->prepare($sql)->execute($data);

Related articles: