Jump to content

Update multiple rows


Fearpig
Go to solution Solved by Barand,

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.