0

i am creating a shopping cart application but there is a problem while testing $_Session['cart'][$product_ID] its is not setting the values according to the $action variable that i called from a url query from a previous page when I activate query for any of the actions the corresponding switch statement is not making any sort of change to Session variable. code is listed below

session_start();
include "db-class.php";
include "functions.php";
$product_ID=$_GET["id"];
$action=$_GET["action"];
$_SESSIONS['cart'][$product_ID]=0;

/////////// testing session variable and action at start/////////
echo "<br>SESSION VAR BEFORE ACTION=".$_SESSIONS['cart'][$product_ID];
echo "<br>ACTION TO TAKE= ".$action."<br>";
//////////end initial testing/////////

$productExisits=productExists($product_ID);
if($product_ID && !$productExisits)
{
die("PRODUCT DOESNOT EXIST");
}
                    switch($action)
                    {
                    case "add":
                    $_SESSION['cart'][$product_ID]++;
                    break;

                    case "remove":
                    if($_SESSION['cart'][$product_ID]>0)
                    {
                    $_SESSION['cart'][$product_ID]--;
                    }

                    if($_SESSION['cart'][$product_ID]=0)
                    {
                    unset($_SESSION['cart'][$product_ID]);
                    }
                    break;

case "empty":
unset($_SESSION['cart'][$product_ID]);
break;
}

/////////////// testing session variable after action/////////
if(!isset($_SESSION['cart'][$product_ID]))
{
echo "<br>THE CART IS EMPTY NOW<br>";
}
echo "SESSION ID IS= ".$_SESSIONS['cart'][$product_ID];

?>

However i want it to change the variable values every time when i perform certain action

idex.html is

<?php
//session_start();

?>
<html>
<head>
</head>
<body>
<div>
<image src="img/b_guitar.jpg" height="100 px" width="270 px" border= "2 px">
<table>
<tr>
<td>
<a href="cart.php?action=add&id=1">add to cart</a> ||<br>
<td>
<a href="cart.php?action=remove&id=1">remove from cart</a> ||<br>
<td>
<a href="cart.php?action=empty&id=1">empty cart</a><br>
</tr>
</table>
</div>
</body>
</html>

1 Answer 1

1

Why should it change? Every time you call the cart update code you reset the cart quantities at line 6 in your above snippet:

$_SESSIONS['cart'][$product_ID]=0;

you'll always end up with 0 items in the cart when the code fires up, regardless of what your later code does to change that quantity.

Sign up to request clarification or add additional context in comments.

2 Comments

but when i remove it it throws an error where and how should i declare the session variable in particular code
check for the value's existence, e.g. if (!isset($_SESSION[....])) { $_SESSION[...] = 0; }. only assign it a 0 value if it DOESN'T exist. otherwise you will continue to trash the quantity value.

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.