Need help with an "update quantities" script for a checkout page

Most websites have this feature, so my objective here should be fairly easy to understand…

On a checkout page, there is a list of items, each with a ‘quantity’ field. At the bottom there is an ‘update quantities’ button.

The name and id of each ‘quantity’ field looks like this:
“quantity[<?php echo $row_cart[‘item_id’]; ?>]”

How do I program a script to update a record, setting the new quantity, for each item in the cart?

just a loop over $_POST[‘quantity’] array

Would this be correct?..

foreach ($_POST['quantity'] as $item_id) {

$quantity = mysql_real_escape_string($_POST['quantity']);

mysql_query("UPDATE cart SET quantity = '$quantity' WHERE item_id = '$item_id'");


The first thing you should do is see what you are dealing with


Then you can try a foreach

foreach($_POST['quantity'] as $k => $v) {

Now you will have some idea of what data you are dealing with, and should be able to see how to make a query of it.

No. this code has no sense. you add equal quantity for the each item. and wrong one.
you need extended version of foreach
foreach ($_POST[‘quantity’] as $item_id => $quantity) {…

foreach ($_POST['quantity'] as $item_id => ) {
  $quantity = intval($quantity);
  $item_id  = intval($item_id);
  mysql_query("UPDATE cart SET quantity = $quantity WHERE item_id = $item_id");

Alright, got it. Thank you very much. =)