1

Im trying to delete an item from a session shopping cart. I used unset(), but somehow it didn't work

Link

<td width="100"><a href="?id=<?php echo $ids;?>&action=delete">
  <?php echo $ids;?></a></td>

Unset

if(isset($_GET['action'])&&($_GET['action']=="delete"))
{
    $new_id=$_GET['id'];
    unset($_SESSION['items'][$new_id]);
}
2
  • A: make sure you're calling session_start() before you try and unset the session var. B: ensure that the structure of your session data matches what you're trying to unset, and debug the $_GET['id'] variable you're trying to use. Commented Jul 25, 2014 at 3:22
  • Try forcing the session updates to be committed using session_write_close();. Commented Jul 25, 2014 at 3:23

2 Answers 2

1

unset session ok look this code result array(1) { ["id"]=> int(10) }

 <?php
  $_SESSION['items']=
  array(
  "id"=>10,
  "new_id"=>6
  );
    unset($_SESSION['items']["new_id"]);
    var_dump($_SESSION['items']);
?>
Sign up to request clarification or add additional context in comments.

1 Comment

the first nesting level of his array is a variable integer, not a static string like "new_id". how do you delete different cart item?
1

Always make sure the id you are passing as a get parameter is set properly, and analyse the structure of your session variable with a var_dump($_SESSION['items']), you should make sure it matches and comment your code as well.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.