gpt4 book ai didi

php - 无法增加/减少 php 中的重载对象或字符串偏移量

转载 作者:行者123 更新时间:2023-11-29 09:17:15 27 4
gpt4 key购买 nike

我正在尝试制作一个简单的 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 设置上启用了注册全局变量。我该如何解决这个问题?

最佳答案

改用 +1:$_SESSION['cart'][$product_id] = $_SESSION['cart'][$product_id] + 1; 应该可以解决问题。

关于php - 无法增加/减少 php 中的重载对象或字符串偏移量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3745128/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com