phunnydoode Posted February 10, 2015 Share Posted February 10, 2015 I have it set up in the cartUpdate.php two cases: 1)add 2)remove. Adding works, removing does not. Any help is greatly appreciated here's what I got: <?php if (!isset($_SESSION)) session_start(); $action = $_GET['action']; $product_id = $_GET['product_id']; switch($action) { //decide what to do case "add": $item = $_POST['item']; $price = $_POST['price']; $qty = $_POST['qty']; $product_id = $_POST['product_id']; $_SESSION['myCart'][] = array("item"=>$item,"qty"=>$qty,"price"=>$price,"product_id"=>$product_id); $_SESSION['myToti'] = $_SESSION['myToti'] + $qty; $_SESSION['myTotp'] = $_SESSION['myTotp'] + ($price * $qty); header("Location:cartView.php"); break; case "remove": unset($_SESSION['myCart'][$product_id]); header("Location:cartView.php"); break; } ?> cartview.php Which has a layout where each item in the shopping cart has a delete link <tr><td colspan='2'><h4>Shopping Cart</td><tr> <tr> <td colspan="2"> <table border="0" padding="0" cellspacing="0"> <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method ="post" name="cartForm" id="cartForm" onsubmit="return checkSubmit();"> <tr class="trColor"> <td width="200" class="nvtxt"><h4>Item Description</h4></td> <td width="100" class="nvtxt"><h4>Item Price</h4></td> <td width="100" class="nvtxt"><h4>Quantity</h4></td> <td width="100" class="nvtxt"><h4>Item Total</h4></td> <td width="100" class="nvtxt"><h4>Delete</h4></td> </tr> <?php foreach($_SESSION['myCart'] AS $temp) { $itmTot = $temp["qty"] * $temp["price"]; ?> <tr> <td><h4><?= $temp["item"] ?></h4></td> <td><h4>$<?= $temp["price"] ?></h4></td> <td><h4><?= $temp['qty'] ?> </h4></td> <td><h4>$<?= $itmTot ?></h4></td> <td><h4><a href="cartUpdate.php?action=remove&product_id=<?= $temp['product_id']?> ">Delete</a></h4></td> </tr> <?php } //} ?> <tr class="trColor"> <td colspan="2"> </td> <td class="nvtxt"><h4>Quantity</td> <td class="nvtxt"><h4>Total</td> </tr> <tr> <td colspan="2" /> <td><h4><?= $_SESSION['myToti'] ?></td> <td><h4>$<?= $_SESSION['myTotp'] ?></td> </tr> <tr class="trColor"><td colspan="4"> </td></tr> <tr> <td colspan="4" align="center" height="100"> <BR> <input type="submit" name="checkout" id="checkout" value="Checkout"> <BR> <input type="submit" name="continue" id="continue" value="Continue Shopping"> <input type="submit" name="submit" id="submit" value="Clear Cart" /> <?php if(isset($_POST['submit'])){ unset($_SESSION['myCart']); header("Location:index.php"); } if(isset($_POST['continue'])){ header("Location:index.php"); } if(isset($_POST['checkout'])){ header("Location:checkout.php"); } ?> </td> </tr> </form> </table> </td> </tr> Quote Link to comment https://forums.phpfreaks.com/topic/294489-delete-from-shopping-cart-not-working/ Share on other sites More sharing options...
mac_gyver Posted February 10, 2015 Share Posted February 10, 2015 $_SESSION['myCart'][] = array("item"=>$item,"qty"=>$qty,"price"=>$price,"product_id"=>$product_id); ^^^ your 'add to cart' code is not using the product_id as the myCart array index, so your 'remove from cart' code isn't finding the correct item in the cart. your add to cart code should be using the product_id as the array index. also, your cart should only contain the product_id (as the array indexes) and the quantity (as the array values.) by passing the item description and the price through the form and then using those values in your cart logic and displaying those values later, you have several security and functional problems in your code. by using only the product_id and quantity, which are both integer values, you can validate them for safety purposes and by using the product_id as the array index, you can easily check if an item has already been added to the cart to avoid duplicate entries in the cart. Quote Link to comment https://forums.phpfreaks.com/topic/294489-delete-from-shopping-cart-not-working/#findComment-1505331 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.