Removing Array Element from 2D Array stored in $_SESSION in PHP-ThrowExceptions

Exception or error:

I am using PHP and mySQL to create a simple shopping cart experience. Products can be added to the cart, but my ‘remove item from cart’ functionality does not work.

I am using an array stored as a $_SESSION variable to store cart items. Each cart item will also be an array containing three elements (name, image src, id). When deleting an item for the cart, it’s id is passed to my removeFromCart.php file where I call array search to get the key value of the item to delete, and then attempt to remove that item using unset().

I believe this is the correct approach as I do not want to remove by index, but rather by key value (corresponding to product id). However, the array element is not actually ‘unset’ from my $_SESSION['cart'] variable. Code follows:

Code snippet from the product page:

<?php session_start();?>

<div class="product-description">
    <h3 class="product-name">Columbia</h3>
    <img src="flags/columbia.gif" alt=""Columbia Ntional Flag">
    <p class="product-price">&dollar;15</p>
    <a href="addToCart.php? id=1">Add to Cart</a>
</div>
<div class="product-description">
    <h3 class="product-name">Mexico</h3>
    <img src="flags/mexico.gif"  alt="Mexico National Flag" >
    <p class="product-price">&dollar;15</p>
    <a href="addToCart.php? id=2">Add to Cart</a>
</div>

Code from addToCart.php:

<?php

session_start();

if(empty($_SESSION['cart'])) {
    $_SESSION['cart'] = array();
}

array_push($_SESSION['cart'], $_GET['id']);

?>

Snippet from cart page:

<?php session_start(); ?>

$items = implode(',', $_SESSION['cart']);   //get items stored in cart 

$result = mysqli_query($conn,"SELECT name, image, id FROM products WHERE id in ($items)");

    $incart = mysqli_fetch_all($result);

    if (!$incart){
        $cartName = "Your Shopping Cart is Empty.";
        $cartImage = '';
    }

foreach ($incart as $cartitem)
echo "
    <table style=\"width:100%; border: 1px solid black; text-align: center\">
    <tr>
        <th style=\"background-color: goldenrod\">Product Name</th>
        <th style=\"background-color: goldenrod\">Image</th>
        <th style=\"background-color: goldenrod\">Remove</th>
    </tr>
    <tr>
        <td style=\"font-size: x-large\"> $cartitem[0] </td>
        <td> 
        <img style= \"width: 250px\" src= \"$cartitem[1]\"> 
        </td>
        <td> 
        <a href='removeFromCart.php? id=$cartitem[2]'>Remove Item</a>
        </td>
        </tr>
        </table>

";
var_dump($incart);
echo "<br>";                   //used to confirm arrays identiical to before 
var_dump($_SESSION['cart']);   // remove item was clicked
echo "<br>";                  
?>

Code from removeItem.php:

<?php

session_start();

$key = array_search($_GET['id'], $_SESSION['cart']);

unset($_SESSION['cart'[$key]]);

header("Refresh:10 url=cart.php");

var_dump($key);          //confirms correct key value 
echo "<br>";
var_dump($_GET['id']);   // confirms correct array element 
How to solve:

RESOLVED: Indeed I did want to use array_splice() instead of unset(). Corrected code for removeItem below:

<?php

session_start();

$key = array_search($_GET['id'], $_SESSION['cart']);

array_splice($_SESSION['cart'], $key, 1);

header("Location: =cart.php");

Answer:

There’s a typo on your removeItem.php page:

Change the following line:

unset($_SESSION['cart'[$key]]);

To

unset($_SESSION['cart'][$key]);

Leave a Reply

Your email address will not be published. Required fields are marked *