Jump to content

Update multiple rows


Fearpig

Recommended Posts

Hello,
I need help updating multiple rows at once
 
Table Columns:
Model_ID, Image

 

I want to update all rows where the image field is empty to have a value of:

'data/Products/' . $model . '.jpg'

I can update one row at a time but I want to upload things in a batch!

I've written a select page

<html>
<body>

<?php

//Conntect to Database
include 'Database_Connection.php';

if ($db_found) { 
	//print "<i>Database Found (" . $db_handle.")</i></br></br>"; 

	$SQL = "SELECT * FROM `oc_product` WHERE image = ''";
	$result = mysql_query($SQL);

	$x = 1;
	
	echo "<table class='Data'><thead><tr><th>Model</th><th>Image</th><th>Image Should Be...</th></tr></thead><tbody>";
	
	while ($db_field = mysql_fetch_assoc($result)){
		
		$model = $db_field['model'];
		$image = $db_field['image'];
		$image_should_be = 'data/Products/' . $model . '.jpg';

		if ($x < 0){ $Table_Row = 'Row_Style1'; }
		else { $Table_Row = 'Row_Style2'; }
			
		print "
		<tr class='$Table_Row'>
		<td>$model</td>
		<td>$image</td>
		<td>$image_should_be</td>
		</tr>";

		$x = $x * (-1);

		}

	echo "</table>";
	
	}
	
else { echo "Whoops!"; }

echo "<a href=Update_Images.php>Update</a>";

?>

</body>
</html> 

My database connection is fine as the user has the correct permissions but when I try to update the images I'm getting something wrong and the records are not updating or giving me an error message.

My idea was to loop through the records using a select query and then update each record.

<html>
<body>

<?php

//Conntect to Database
include 'Database_Connection.php';

if ($db_found) { 
	//print "<i>Database Found (" . $db_handle.")</i></br></br>"; 

	$SQL = "SELECT * FROM `oc_product` WHERE image = ''";
	$result = mysql_query($SQL);
	
	while ($db_field = mysql_fetch_assoc($result)){
		
		$model = $db_field['model'];
		echo "$model<br>";
		
		$image_should_be = 'data/Products/' . $model . '.jpg';	
		$SQL_UPDATE = "UPDATE `oc_product` SET image = '$image_should_be' WHERE model = $model";
		$result_update = mysql_query($SQL_UPDATE);
		}
	
	}
	
else { echo "Whoops!"; }

echo "<a href=test.php>Test</a>";

?>

</body>
</html> 

I added in the...

echo "$model<br>";

...just to see if the page was doing anything and I now get a list of model numbers but no updates in the table.

Link to comment
https://forums.phpfreaks.com/topic/277680-update-multiple-rows/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.