Hello All,
I'm creating a shopping cart & I was adapting several aspects while generally I'm following the codes in the book. Here is where I get my problem:
pg. 239 on updating carts:
} elseif (isset($_POST['quantity'])) { // Update quantities in the cart.
// Loop through each item:
foreach ($_POST['quantity'] as $sku => $qty) {
// Parse the SKU:
list($sp_type, $pid) = parse_sku($sku);
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 loop.
}// End of main IF.
I changed it this way, primarily to avoid using stored procedures (for some reason I can't save my stored procedures):
elseif (isset($_POST['quantity'])) { // Update quantities in the cart.
// Loop through each item:
foreach ($_POST['quantity'] as $product_code => $qty) {
if (isset($product_code)) {
// Determine the quantity:
$qty = (filter_var($qty, FILTER_VALIDATE_INT, array('min_range' => 0)) !== false) ? $qty : 1;
if ($qty > 0) {
$sql = "UPDATE cart SET quantity=qty, date_modified=NOW() WHERE product_code='$product_code' AND user_session_id='$uid'";
} elseif ($qty == 0) {
$sql = "DELETE FROM cart WHERE product_code='$product_code' AND user_session_id='$uid'";
}
// Update the quantity in the cart:
$result = $conn->query($sql) or die(mysqli_error());
}
} // End of FOREACH loop.
}// End of main IF.
My product does not need parsing as Larry's 'sku'. My question is on the variable $sku (Larry's book) and the respective $product_code (my code). If I run this, the update function does not work because $product_code is unrecognized. The error generated is that $result is empty (there is no result).
When I look at Larry's code, $sku is also previously undefined.
The related part of this code is this:
...
echo '<tr><td>' . strtoupper($display[0]).' '.strtoupper($display[1]). '</td>
<td align="right"><input type="text" name="quantity[' . $row['product_code'] . ']" value="' . $row['quantity'] . '" size="2" class="small" /></td>
What should I change to make this work? Thank you in advance for the help.