1

I want to add products to my array, but every time I add a product, it replaces the old one in the array.

When I add 2 things, it works fine:

Array ( [0] => 2 [1] => 8 )

But when I try to add a third item or more, it replaces the second item in the array:

Array ( [0] => 2 [1] => 10 )

Here is my code:

  session_start();

  if(isset($_POST['inCart']))
  {
       $id = $_POST['id'];

       if(!empty($_SESSION['cart']))
       {     
          $session = $_SESSION['cart'];
          $session[] = $id;

          print_r($session);
       }

       else
       {
          $_SESSION['cart'] = array($id); 
       }
  }

1 Answer 1

1

As i see you forgot set SESSION with new data

if(!empty($_SESSION['cart']))
       {     
          $session = $_SESSION['cart'];
          $session[] = $id;

          $_SESSION['cart'] = $session;

          print_r($session);
       }
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much, was a stupid mistake of me. I also shortened my code now I know my mistake session_start(); if(isset($_POST['inWagen'])) { $id = $_POST['id']; if(isset($_SESSION['winkelwagen'])) { $_SESSION['winkelwagen'][] = $id; } else { $_SESSION['winkelwagen'] = array($id); } }

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.