This question already has answers here:
Answer
Solution:
If you have specific values for each column that will never appear in the data, you can check for those. For instance, if name can never be DO NOT UPDATE
, have your prepared statement be:
UPDATE homes SET ..., name = if(?='DO NOT UPDATE',name,?) WHERE id=?
and pass the value for name (which may be DO NOT UPDATE
) to execute twice:
$stmt2->execute([..., $row['name'], $row['name'], $row['id']]);
Answer
Solution:
I believe you do not want to separately process the $figures array so you want to do it in a single loop.
So just add a second sql prepared statement (you do not mind, right?) and then use a if-then-else to determine which prepared statement to execute
<?php
$figures = [ ['age' => 'foo1', 'name' => 'DO NOT UPDATE', 'id' => '1'], ['age' => 'foo1', 'name' => 'bar1', 'id' => '2'], ['age' => 'foo2', 'name' => 'bar2', 'id' => '3']];
$stmt = $pdo->prepare("UPDATE homes SET age = ?, name = ? where id=?");
$stmt2 = $pdo->prepare("UPDATE homes SET age = ? where id=?");
foreach ($figures as $row) {
if ($row['name']!='DO NOT UPDATE'){
$stmt->execute([$row['age'], $row['name'], $row['id']]);
} else {
$stmt2->execute([$row['age'], $row['id']]);
}
}
?>
Please amend the prepared statements to suit your real needs
Source