-2
下面的代碼將只更新我的購物車的最後一項。我在循環中掙扎了一下,我只是卡住了。上cart.html購物車 - 只更新1條目
if (isset($_POST['quantity'])) { // Update quantities in the cart
foreach ($_POST['quantity'] as $k => $qty) { //Full name on form: name="quantity[' . $row['prodid'] . ']"
$pid = $_POST ['prodid'];
$sp_type = $_POST ['cat'];
if (isset($sp_type, $pid)) {
// Determine the quantity:
$qty = (filter_var($qty, FILTER_VALIDATE_INT, array('min_range' => 0)) !== false) ? $qty : 1;
// Update the quantity in the cart:
$r = mysqli_query($dbc, "CALL update_cart('$uid', '$sp_type', $pid, $qty)");
}
} // End of FOREACH
}
代碼:
這是爲HTML形式的購物車中的代碼:
<input type="hidden" name="prodid[' . $row['prodid'] . ']" value="' .$row ['prodid'] .'" /></td>
<td align="center"><input type="text" name="quantity[' . $row['prodid'] . ']" value="' . $row['quantity'] . '" size="2" class="small" /></td>
<td align="right">£' . $price . '</td>
<td align="right">£' . number_format($subtotal, 2) . '</td>
<td align="right"><a href="/wishlist.php?id=' . $row['prodid'] . '&action=move&qty=' . $row['quantity'] .'&cat=' .$row ['cat_id'] .'">
你有數量上的循環,但不是的產品編號,我會重新安排我的表單元素的名稱更好地適應循環。例如:name =「quantity [$ prodid]」,所以這個數量的關鍵成爲產品編號 – Scuzzy
你能否提供你的$ _POST params的var_dump? –
我到底怎麼做?對不起,我是一個初學者(自學)。 – user1810442