我正在尝试制作一个简单的php购物车。我不知道如何自己制作一个,所以我尝试搜索并发现这个最容易理解的像我这样的初学者:http://jameshamilton.eu/content/simple-php-shopping-cart-tutorial 唯一的问题是我得到了这行代码标题上提到的错误:
$_SESSION['cart'][$product_id]++;
以下是整个代码:
$product_id = $_GET['id']; //the product id from the URL
$action = $_GET['action']; //the action from the URL
//if there is an product_id and that product_id doesn't exist display an error message
if($product_id && !productExists($product_id)) {
die("Error. Product Doesn't Exist");
}
switch($action) {
case "add":
$_SESSION['cart'][$product_id]++; //add one to the quantity of the product with id $product_id
break;
case "remove":
$_SESSION['cart'][$product_id]--; //remove one from the quantity of the product with id $product_id
if($_SESSION['cart'][$product_id] == 0) unset($_SESSION['cart'][$product_id]); //if the quantity is zero, remove it completely (using the 'unset' function) - otherwise is will show zero, then -1, -2 etc when the user keeps removing items.
break;
case "empty":
unset($_SESSION['cart']); //unset the whole cart, i.e. empty the cart.
break;
}
?>
我还在php设置上启用了注册全局变量。我该怎么做才能解决这个问题?
答案 0 :(得分:2)
使用+1代替:$_SESSION['cart'][$product_id] = $_SESSION['cart'][$product_id] + 1;
应该可以做到这一点。
答案 1 :(得分:0)
关闭register_globals
会解决问题。你不应该使用它,它提出了一个主要的安全问题(在某些情况下很容易被利用)。