PHP Code To Update Multiple Rows of A Table in Mysql Database
PHP Code To Update Multiple Rows of A Table in Mysql Database
<?php
// Update values we got from somewhere
$update_values = Array(
'1034786' => Array('column1' => 0, 'column2' => NULL, 'column3'=> 'Text One'),
'1037099' => Array('column1' => 0, 'column2' => 1034789 , 'column3'=> 'Text
Two'),
'1034789' => Array('column1' => 3, 'column2' => 1034789 , 'column3'=> 'Text
Three')
);
// Add a default case, here we are going to use whatever value was already in the
field
foreach($columns as $column_name => $query_part){
$columns[$column_name] .= " ELSE `$column_name` END ";
}
// Build the WHERE part. Since we keyed our update_values off the database keys,
this is pretty easy
$where = " WHERE `id`='" . implode("' OR `id`='", array_keys($update_values)) .
"'";