Я пытаюсь сделать простую корзину для покупок 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;
должен сделать трюк.
Отключение register_globals
приведет к его исправлению. Вы все равно не должны его использовать, это представляет серьезную проблему безопасности (и в некоторых случаях ее легко использовать).