0% found this document useful (0 votes)
210 views

PHP Code To Update Multiple Rows of A Table in Mysql Database

This PHP code shows how to update multiple rows in a MySQL database table at once. It builds a SQL query that uses a CASE statement for each column being updated, allowing different values to be set for different rows based on their IDs. The column values to update are provided in an associative array, which is then used to dynamically generate the SQL with the correct values for each row. The query is then executed to perform the multiple row database update in one operation.

Uploaded by

Thomas Chinyama
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
210 views

PHP Code To Update Multiple Rows of A Table in Mysql Database

This PHP code shows how to update multiple rows in a MySQL database table at once. It builds a SQL query that uses a CASE statement for each column being updated, allowing different values to be set for different rows based on their IDs. The column values to update are provided in an associative array, which is then used to dynamically generate the SQL with the correct values for each row. The query is then executed to perform the multiple row database update in one operation.

Uploaded by

Thomas Chinyama
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

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')
);

// Start of the query


$update_query = "UPDATE `table` SET ";

// Columns we will be updating


$columns = Array('column1' => '`column1` = CASE ', 'column2' => '`column2` =
CASE ', 'column3' => '`column3` = CASE ');

// Build up each columns CASE statement


foreach($update_values as $id => $values){
$columns['column1'] .= "WHEN `id`='" . mysql_real_escape_string($id) . "' THEN
'" . mysql_real_escape_string($values['column1']) . "' ";
$columns['column2'] .= "WHEN `id`='" . mysql_real_escape_string($id) . "' THEN "
. ($values['column2'] === NULL ? "NULL" :
"'".mysql_real_escape_string($values['column1'])."'") . " ";
$columns['column3'] .= "WHEN `id`='" . mysql_real_escape_string($id) . "' THEN
'" . mysql_real_escape_string($values['column3']) . "' ";
}

// 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)) .
"'";

// Join the statements with commas, then run the query


$update_query .= implode(', ',$columns) . $where;
mysql_query($update_query) or die(mysql_error());
?>

You might also like