store data in session??

They have: 164 posts

Joined: Nov 2001

hi, i have a shopping cart where user need not login when they add product into the cart. a same product can be only added to the cart once. how to check in order to make sure they only can add once??

this is my code..

<?php
if (isset($HTTP_POST_VARS['pid']['pid']))
    {
       
$_SESSION['pid'][] = $HTTP_POST_VARS['pid']['pid'];
    }

    foreach(
$_SESSION['pid'] as $pid)
    {
        echo \
"pid:$pid<br>\";
       
$select_pro = mysql_query(\"select * from sc_product where pid='$pid'\");
       
$num = mysql_num_rows($select_pro);
       
        if (
$num > 0)
        {
            while (
$temp = mysql_fetch_array($select_pro))
            {
               
$pid = $temp['pid'];
               
$sku = $temp['sku'];
               
$pro_name = $temp['pro_name'];
               
$pro_cat = $temp['pro_cat'];
               
$pro_brand = $temp['pro_brand'];
               
$our_price = $temp['our_price'];
               
$retail_price = $temp['retail_price'];
               
$pro_weight = $temp['pro_weight'];
                           
                echo \"<tr><td width=5% align=center><input type=text name=qty value=\\"
$qty\\" size=2 class=ft1></td>\";
                echo \"<td width=35% align=center>
$pro_name</td>\";
                echo \"<td width=15% align=center>
$our_price</td>\";           
                echo \"<td width=15% align=center>
$amount</td></tr>\";
            }
        }
}
?>

and another thing is, i try to echo the pid below the foreach:

<?php
echo \"pid:$pid<br>\";
?>

and as a result, i got this when i add the first product in my cart:

pid:
pid:2
'

and when i add the second product,

pid:
pid:2
pid:
pid:4
'

why there is an extra pid everytime i add a product? anything wrong with tat??

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

try this:
print_r($HTTP_POST_VARS);
print_r($_SESSION);

Hopefully, something (a patern) will stand out and you'll know where the problem is.

They have: 164 posts

Joined: Nov 2001

i got this as a result

Array ( [go] => Add To Shopping Cart [form] => Array ( [sku] => xxx ) [pid] => Array ( [pid] => 3 ) ) Array ( [pid] => Array ( [0] => ) )
'

wat does this mean??

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

It's much easier to read if you view source... then it's all spaced out:

Array
(
    [go] => Add To Shopping Cart
    [form] => Array
        (
            [sku] => xxx
        )
    [pid] => Array
        (
            [pid] => 3
        )
)
Array
(
    [pid] => Array
        (
            [0] =>
        )
)
'From that, it looks like your vars are:
$HTTP_POST_VARS[go] = "Add To Shopping Cart";
$HTTP_POST_VARS[form][sku] = "xxx";
$HTTP_POST_VARS[pid][pid] = 3;

$_SESSION[pid][0] = NULL;
'It looks like the problem is here:
$_SESSION['pid'][] = $HTTP_POST_VARS['pid']['pid'];

But I don't see anything wrong with that syntatically.
I've never seen or tried to assign anything to $_SESION directly,
and I can't find anything in the manual addressing this.
Try using a non-super-global array.

Mark Hensler
If there is no answer on Google, then there is no question.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.