-1

I'm currently getting my cart sorted on my online store. I have the cart set as an array which is a session variable. I can get items added to the cart easy enough but I'm having trouble removing them. I've tried removing them using unset on the session variable or getting the session variable, updating it as a local variable and then setting the updated version as the session variable yet none of them have worked.

The value $_SESSION["cart"] is an array with items that are in the cart. $_GET["account"] is the index of the item they want to remove from the cart. Here is my current code:

if (isset($_GET["account"])) {

    $accountnumber = $_GET["account"];
    $cart = $_SESSION["cart"];
    unset($cart[$accountnumber]);
    $_SESSION["cart"] = $cart;

}

Please let me know what's wrong with it.

Thanks

Hydrone
  • 73
  • 8

3 Answers3

0

you can unset the session like this

unset( $_SESSION["cart"][$_GET["account"]]);

Note : Don't forgot to start the session on top of page

JYoThI
  • 11,977
  • 1
  • 11
  • 26
0

No need to reference the session with another variable.

unset($_SESSION["cart"][$accountnumber]);

such is enough.

wayzz
  • 585
  • 6
  • 23
0

Is there another session exist in your website? If not you can try session_destroy(); otherwise, try this.

if (isset($_GET["account"])) {
   $key=array_search($_GET['account'],$_SESSION['cart']);
   if($key!==false){
      unset($_SESSION['cart'][$key]);
   }
}

refer by link

Ilove112
  • 94
  • 1
  • 10