Explosion
Explosion

Reputation: 75

My $_GET value is not getting stored in $_SESSION array

I have a script that works a cart-mechanism. I want to store a $_GET value in a $_SESSION array I just made , but it stores it only once. The other indexes are not getting added.

Here is my code:

 <?php
 session_start();  
 require_once 'db_connect.php';

 $_SESSION['product_name']=array ();
 array_push($_SESSION['product_name'],$_GET["p_name"]);
 ?>

Upvotes: 1

Views: 72

Answers (2)

Nilesh Lathe
Nilesh Lathe

Reputation: 152

if(isset($_SESSION['cartitems'])) { #find index of an array if it exits then do not add else add to cart and then if (!array_key_exists($pr_id,$_SESSION['cartitems'])) { $_SESSION['cartitems'][$pr_id]['id'] = $pr_id; $_SESSION['cartitems'][$pr_id]['quantity'] = $quantity; } } else { $_SESSION['cartitems'][$pr_id]['id'] = $pr_id; $_SESSION['cartitems'][$pr_id]['quantity'] = $quantity; } $arr = array(); $arr['count'] = count($_SESSION['cartitems']); $arr['message'] = 'Item successFully added'; echo json_encode($arr);

Upvotes: -1

aidinMC
aidinMC

Reputation: 1436

you unset $_SESSION['product_name'] every time by this line:

$_SESSION['product_name']=array ();

change it like below:

if(!isset($_SESSION['product_name'])
    $_SESSION['product_name']=array ();

Upvotes: 3

Related Questions