我正在创建一个购物车应用程序,但在测试 $_Session['cart'][$product_ID] 时出现问题,它没有根据我从上一页的 url 查询中调用的 $action 变量设置值激活对任何操作的查询,相应的 switch 语句不会对 Session 变量进行任何类型的更改。代码如下
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];
?>
但是我希望它每次执行某些操作时都更改变量值
idex.html 是
<?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>