I have a table in my database with a few fields. I am using this form to select data from this table:
<form id="#" name="#" method= "post" action="cms_order_detail.php">
<table width="100%" border="1" align="center">
<th> Item </th>
<th> Price </th>
<th> Active </th>
<tr>
<?php
$sql= mysql_query("SELECT * FROM `product` where `item_id` = '$id' AND `type`=0 ");
while($row= mysql_fetch_array($sql)){
$price = $row['price'];
$item_id = $row['item_id'];
$size = $row['size'];
?>
<td class="price"> <?php echo $size;?> </td>
<td class="price" width="1px">
<input type="number" name="pro_price" id="pro_price" value="<?php echo $price;?>" style="width:4.2em; " >
</td>
<td align="center">
<input type="number" name="onof" id="onof" value="<?php echo $row['active'];?>" style="width:2.5em; " min="0" max="1" title="1= active and 0= De-active">
</td>
</tr>
<?php }?>
</table>
<button class="updateBtn"> Update </button>
<div style="clear:both"> </div>
</form>
I have to update the table using MySQL and PHP. How to do it? The screenshot of my web page is as below.
When I click up update button the above rows should be updated. Please see the image for clear message.
Front view code(for display):
<form id="frm_update" name="frm_update" method= "post" action="cms_order_detail.php">
<table width="100%" border="1" align="center">
<th> Item </th>
<th> Price </th>
<th> Active </th>
<tr>
<?php
$sql= mysql_query("SELECT * FROM `product` where `item_id` = '$id' AND `type`=0 ");
while($row= mysql_fetch_array($sql)){
$price = $row['price'];
$item_id = $row['item_id'];
$size = $row['size'];
?>
<td class="price"> <?php echo $size;?> </td>
<td class="price" width="1px">
<input type="number" name="pro_price[]" id="pro_price" value="<?php echo $price;?>" style="width:4.2em; " >
</td>
<td align="center">
<input type="number" name="onof[]" id="onof" value="<?php echo $row['active'];?>" style="width:2.5em; " min="0" max="1" title="1= active and 0= De-active">
</td>
<input type="hidden" name="item_id[]" value="<?php print($item_id);?>"/>
</tr>
<?php }?>
</table>
<input type='submit' name='submit' Value='Update' class="updateBtn"/>
<div style="clear:both"> </div>
</form>
Back end code(For update):
<?php
/**
* YOUR CONNECTION...
*$conn = mysql_connect("localhost","root","");
*mysql_select_db("phppot_examples",$conn);
**/
if(isset($_POST["submit"]) && $_POST["submit"]!="") {
$item_idCount = count($_POST["item_id"]);
for($i=0; $i<$item_idCount; $i++) {
mysql_query("UPDATE users set price='" . $_POST["pro_price"][$i] . "', active='" . $_POST["onof"][$i] . "' WHERE item_id ='" . $_POST["item_id"][$i] . "'");
}
header("Location:MYFRONTURL.php");
}
?>
Warning: This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used.
UPDATE:
Used <input type='submit' name='submit' Value='Update' class="updateBtn"/>
instead <button class="updateBtn"> Update </button>
.
This should work!
For that you have to make changes in above code because, for this code we can not find which value will be associated with which row?
<?php
$sql= mysql_query("SELECT * FROM `product` where `item_id` = '$id' AND `type`=0 ");
while($row= mysql_fetch_array($sql)){
$price= $row['price'];
$item_id= $row['item_id'];
$size= $row['size'];
?>
<td class="price"><?php echo $size;?> </td>
<td class="price" width="1px">
<input type="number" name="pro_price-$row['item_id']" id="pro_price-$row['item_id']" value="<?php echo $price;?>" style="width:4.2em; " >
</td>
<td align="center">
<input type="number" name="onof_$row['item_id']" id="onof_$row['item_id']" value="<?php echo $row['active'];?>" style="width:2.5em; " min="0" max="1" title="1= active and 0= De-active">
</td>
</tr>
<?php }?>
Now you get all the value with different name, when you print($_REQUEST); you will get all then value of with different name. you have to extract item_id from each variable using - and run update query in for loop or while loop.
You need all IDs. You can do that by issuing, in the foreach, this:
<input type="hidden" name="id[]" value="<?php echo $row['item_id']; ?>" />
This will result in HTML:
<input type="hidden" name="id[]" value="1" />
...
<input type="hidden" name="id[]" value="6" />
and in $_POST['id'] being an array containing 1,2 and so on.
You could also put in HTML:
<input type="number" name="pro_price[<?php echo $row['item_id']; ?>]" />
<input type="number" name="onof[<?php echo $row['item_id']; ?>]" />
so that $pro_price=$_POST['pro_price'],$onof=$_POST['onof'] would be a vector with the same keys as the values of id, and therefore:
foreach($id as $id1)
{
// Pseudocode
UPDATE... SET pro_price=$pro_price[$id1],onof=$onof[$id1] WHERE id = $id1;
}
This way you have one SUBMIT, and multiple UPDATEs.