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?
system
March 7, 2010, 6:35am
2
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'");
}
hash
March 7, 2010, 9:02am
4
No.
The first thing you should do is see what you are dealing with
var_dump($_POST['quantity']);
Then you can try a foreach
foreach($_POST['quantity'] as $k => $v) {
var_dump($k);
var_dump($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.
system
March 7, 2010, 9:05am
5
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");
}
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. =)