How to update multiple rows in mysql with php

Hi

I’m trying to get this script to work, it almost works but doesn’t update ALL the fields after you click submit. Two fields don’t display at all (the ones that don’t update) . One field isn’t suppose to update but should display i.e. record_number, the other field i.e. Avalibity, doesn’t display or update (it suppose to update).

Here is the first page:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head>
<style type="text/css">

body {
	margin:50px 0px; padding:0px;
	text-align:center;
        font:13px Tahoma,Geneva,sans-serif
	}
	
#content {
	width:1000px;
	margin:0px auto;
	text-align:left;
	padding:15px;
	border:1px dashed #333;
	background-color:#eee;
	}
</style>
<link rel="icon" href="http://www.mysite.com/favicon.ico" type="image/vnd.microsoft.icon" />
</head>
<body>
<div id='content'><h3><center>Update Product Infomation</center></h3>
<?php
// connect to the database
mysql_connect(localhost,username,password);

// select the database
mysql_select_db(db_name) or die("Unable to select database");

// run the query and put the results in an array variable called $result
$result = mysql_query("SELECT * FROM Products ORDER BY 'record_number', 'Product_Manu_URL', 'manufactures_product_code', 'Avalibity'");
// find out how many records there are to update
$size = count($_POST['record_number']);

// start a counter in order to number the input fields for each record
$i = 0;
print "<table width='100%' border='0' cellspacing='1' cellpadding='0'><tr><td>";
// open a form
print "<form name='namestoupdate' method='post' action='update.php'>
<table width='100%' border='0' cellspacing='1' cellpadding='1'><tr>

<td align='center'><strong>Record Number</strong></td>
<td align='center'><strong>Manufactures Product Code</strong></td>
<td align='center'><strong>Product Manu URL</strong></td>
<td align='center'><strong>Avalibity</strong></td>
</tr>\
";

// start a loop to print all of the courses with their book information
// the mysql_fetch_array function puts each record into an array. each time it is called, it moves the array counter up until there are no more records left
while ($Update = mysql_fetch_array($result)) {
print "</tr>\
";
// assuming you have three important columns (the index (id), the course name (course), and the book info (bookinfo))
  // start displaying the info; the most important part is to make the name an array (notice bookinfo[$i])
  print "<td align='center'><p>{$Update['record_number']}</p></td>\
";
  print "<td align='center'><input type='text' name='manufactures_product_code[$i]' value='{$Update['manufactures_product_code']}' /></td>";

  print "<td align='center'><input type='text' size='40' name='Product_Manu_URL[$i]' value='{$Update['Product_Manu_URL']}' /></td>\
";
  print "<td align='center'><input type='text' size='40' name='Avalibity[$i]' value='{$Update['Avalibity']}' /></td>\
";
print "</tr>\
";
// add 1 to the count, close the loop, close the form, and the mysql connection
++$i;
}
print "<tr>
<td colspan='4' align='center'><input type='submit' value='submit' />";
print "</td>
</tr>
</table>
</td>
</tr>
</form>
</table>";
mysql_close();
?><br /><br /><div></body>
</html>

here is the 2nd page i.e. update.php

&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;&lt;head&gt;
&lt;style type="text/css"&gt;

body {
	margin:50px 0px; padding:0px;
	text-align:center;
        font:13px Tahoma,Geneva,sans-serif
	}
	
#content {
	width:1000px;
	margin:0px auto;
	text-align:left;
	padding:15px;
	border:1px dashed #333;
	background-color:#eee;
	}
&lt;/style&gt;
&lt;link rel="icon" href="http://www.mysite.com/favicon.ico" type="image/vnd.microsoft.icon" /&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div id='content'&gt;&lt;h3&gt;&lt;center&gt;Success! &lt;/center&gt;&lt;/h3&gt;
&lt;table width='100%' border='0' cellspacing='1' cellpadding='0'&gt;&lt;tr&gt;&lt;td&gt;


&lt;table width='100%' border='0' cellspacing='1' cellpadding='1'&gt;
&lt;tr&gt;

&lt;td align='center'&gt;&lt;strong&gt;Record Number&lt;/strong&gt;&lt;/td&gt;
&lt;td align='center'&gt;&lt;strong&gt;Manufactures Product Code&lt;/strong&gt;&lt;/td&gt;
&lt;td align='center'&gt;&lt;strong&gt;Product Manu URL&lt;/strong&gt;&lt;/td&gt;
&lt;td align='center'&gt;&lt;strong&gt;Avalibity&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;?php
// connect to the database and select the correct database
mysql_connect(localhost,username,password);
mysql_select_db(db_name) or die("Unable to select database");

// find out how many records there are to update
$size = count($_POST['Avalibity']);

// start a loop in order to update each record
$i = 0;
while ($i &lt; $size) {
// define each variable
$Product_Manu_URL = $_POST['Product_Manu_URL'][$i];
$Record_Number = $_POST['record_number'][$i];
$manufactures_product_code = $_POST['manufactures_product_code'][$i];
$Avalibity = $_POST['Avalibityl'][$i];

// do the update and print out some info just to provide some visual feedback

$query = "UPDATE `Products` SET `Avalibity` = '$Avalibity', `Product_Manu_URL` = '$Product_Manu_URL', `manufactures_product_code` = '$manufactures_product_code' WHERE `record_number` = '$Record_Number' LIMIT 1";
mysql_query($query) or die ("Error in query: $query");
print "

&lt;/tr&gt;
&lt;td align='left'&gt;&lt;p&gt;$Record_Number&lt;/p&gt;&lt;/td&gt;
&lt;td align='left'&gt;$manufactures_product_code&lt;/td&gt;
&lt;td align='left'&gt;$Product_Manu_URL&lt;/td&gt;
&lt;td align='left'&gt;$Avalibity&lt;/td&gt;
&lt;/tr&gt;
";
++$i;
}
mysql_close();
?&gt;
&lt;tr&gt;
&lt;td colspan='4' align='center'&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/td&gt;
&lt;/tr&gt;

&lt;/table&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;

It works very well except I need it to be able to update more fields.

Thanks

It’s because you have typos in the field names that need to be fixed. Spelling some of these words correctly would probably help you stay consistent.

Thanks!

Spelling has never been one of my strong points. :slight_smile:

Everything works fine now but “Record Number” doesn’t show up on the 2nd page. It’s not super important that it does (would be nice) as it doesn’t update but only helps me to use ctrl+f to find things.

You are setting it to $_POST[‘record_number’][$i], but you did not create a field in the posted form called record_number, so it’s not going to be in $_POST. This also means your UPDATE query doesn’t know which row to update.

Add it as a hidden input to the first form.

Thanks

I always seem to come away from sitepoint forums with solutions!