Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
Hi all,

I have two pages that I'm trying to convert to one. The original lets you enter the info and posts to the update page which in turn updates the DB.

I want all this to happen on the same page, but I think I'm having trouble as the page is not posting maybe??

Update multiple rows in mysql<br>

<?php
error_reporting(E_ALL);

mysql_connect("","","");
mysql_select_db("") or die("Unable to select database");

$sql = "SELECT  product_id, product_sku, product_name, product_in_stock, product_in_stock_cardiff FROM mik1vm_product";

$result = mysql_query($sql) or die($sql."<br/><br/>".mysql_error());

$i = 0;


?>
<table width="500" border="0" cellspacing="1" cellpadding="0">
<form name="form1" method="post" action="">
<tr>
<td>
<table width="500" border="0" cellspacing="1" cellpadding="0">


<tr>
<td align="center">Product Id</td>
<td align="center">SKU</td>
<td align="center">Name</td>
<td align="center">In Stock</td>
<td align="center">In Stock Cardiff</td>
</tr>
<?php
while ($products = mysql_fetch_array($result)){
?>


<tr>
<td align="center"><? $products['product_id']; ?><? echo $products['product_id']; ?></td>
<td align="center"><input name='product_sku' type="text" id="product_sku" value="<? echo $products['product_sku']; ?>"></td>
<td align="center"><input name="product_name" type="text" id="product_name" value="<? echo $products['product_name']; ?>"></td>
<td align="center"><input name="product_in_stock" type="text" id="product_in_stock" value="<? echo $products['product_in_stock']; ?>"></td>
<td align="center"><input name="product_in_stock_cardiff" type="text" id="product_in_stock_cardiff" value="<? echo $products['product_in_stock_cardiff']; ?>"></td>
</tr>
<?php
}
?>
<tr>
<td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</td>
</tr>
</form>
</table>
<?php
// Update on submit...
if(isset($_POST['Submit']) && $_POST['Submit'] == 'Submit'){
	
	$size = count($_POST['product_id']);

$i = 0;
while ($i < $size) {
	$product_sku= $_POST['product_sku'][$i];
	$product_id = $_POST['product_id'][$i];
	
	$query = "UPDATE mik1vm_product SET product_sku = '$product_sku' WHERE product_id = '$product_id' LIMIT 1";
	mysql_query($query) or die ("Error in query: $query");
	echo "$product_sku<br /><br />Updated!<br /><br />";
	++$i;
}
}

mysql_close();
?>


I'm very new to PHP so please bear with me...

ERROR -
Notice: Undefined index: product_id in /home/itjustwo/public_html/test/test.php on line 58

Thanks.
Posted
Updated 22-Apr-12 9:51am
v2

Have a look at this[^]
 
Share this answer
 
The error is because of you are not submitting any element in the name of product_id

and you can create the html hidden element
HTML
<input type="hidden" name="product_id[]" value="<?php echo $products['product_id']; ?>"></input>

so this will be array so u can access all the values.

and for other elements also create a array like

HTML
<input name="product_name" type="text" id="product_name[]" value="<? echo $products['product_name']; ?>"></input>


I think this will solve your problem.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900