This question might probably have been asked somewhere but I just can't seem to find it after much searching.
I have a basic question on how I can prevent the last SQL statement in my code below from executing if it caused any row/data in the table to change.
$query = 'SELECT pdt_code FROM `Products`';
$stmt = $con->query($query);
while ($obj = $stmt->fetch()) {
$pdt_code[] = $obj['pdt_code'];
}
$args = implode(',',
array_map(function($el) {
return '('.implode(',', $el).')';
},
array_chunk(array_fill(0, count($pdt_code), '?'), count($pdt_code))
)
);
$query = 'ALTER TABLE `MapProduct` CHANGE product_code product_code SET '.$args.' COLLATE utf8mb4_unicode_ci NOT NULL';
$stmt = $con->prepare($query);
$stmt->execute($pdt_code); // how do I prevent this line from executing if it causes the data in table `MapProduct` to change?
I am trying to execute this from PHP where I query another table for updates to product codes and update the SET() accordingly. However, I had a horrific experience of losing all my data because the update was not properly executed and all the data in product_code got wiped.