new guy here! I am building a custom shopping cart driven by mysql and i am trying to update my cart item by item in terms of quantity. It seems that i am doing something wrong because when i try to update the quantity it only update the last item. I am posting the code below. Any help would be appreciated. Thanks in advance.
1.cart.php:
$sql = "select * from orders";
$result = mysql_query($sql);
$num = mysql_num_rows($result);
echo "Στοιχεία: ".$num;
?>
<form name="cart" method="post" action="updatecart.php">
<table border="2">
<tr>
<td>Α/Α</td>
<td>img_name</td>
<td>Reprints</td>
<td>Color</td>
</tr>
<?php
if($num){
while ($row = mysql_fetch_array($result)){
?>
<tr>
<td><?php echo $row['item_id']; ?></td>
<td><?php echo $row['img_name']; ?></td>
<td><input type="number" name="quantity" value="<?php echo $row['quantity']; ?>"></td>
<input type="hidden" name="item_id" value="<? echo $row['item_id']; ?>">
<td><?php echo $row['color']; ?></td>
</tr>
<?php
}
}
?>
</table>
<input type="submit" name="update" value="Update Cart" />
<input type="button" name="2checkout" value="Proceed to Checkout" />
</form>
2.updatecart.php
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<?php
$database_name = "vog";
$conn = mysql_connect("localhost","root","toor");
mysql_select_db($database_name);
$num = 2; //na to ferw me session meta edw!
if(isset($_POST['update'])){
$item_id = $_POST['item_id'];
$i=1;
while($i<=$num){
$item_id = $_POST['item_id'][$i];
$quantity = $_POST['quantity'];
$sql2 = "update orders SET quantity = '$quantity' where item_id = '$item_id' ";
$result2 = mysql_query($sql2) or die ("Error in query: $result2");
$i++;
}
}
if(isset($result2)){
header("Location:cart.php");
}
?>
So far it updates just the last record.