rogerjarema Posted July 25, 2013 Share Posted July 25, 2013 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. Link to comment Share on other sites More sharing options...
rogerjarema Posted July 26, 2013 Author Share Posted July 26, 2013 Solved. I just missed the "$" in the SQL query: "qty" which should've been "$qty" Link to comment Share on other sites More sharing options...
HartleySan Posted July 26, 2013 Share Posted July 26, 2013 Nice find. Thanks for sharing your solution. Link to comment Share on other sites More sharing options...
rogerjarema Posted July 26, 2013 Author Share Posted July 26, 2013 Thanks, HartleySan. I'm a relative newbie to programming, but I know these annoying little things are the bread & butter of programming. Link to comment Share on other sites More sharing options...
Larry Posted July 26, 2013 Share Posted July 26, 2013 Also, the e-commerce book does assume complete comfort with PHP and MySQL. If you don't have that, the book may be a bit too challenging for you at this point. Link to comment Share on other sites More sharing options...
Recommended Posts