Jump to content

Recommended Posts

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>

post-174346-0-21239600-1423550966_thumb.png

Link to comment
https://forums.phpfreaks.com/topic/294489-delete-from-shopping-cart-not-working/
Share on other sites

$_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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.