PHP 会话错误
我试图制作一个购物车并从网络上获取了一个代码..
<?php
session_start();
require_once 'class/Item.php';
$product_id = $_REQUEST['i_id'];
$action = $_REQUEST['action'];
$item= new Item();
if($product_id && !$item->productExists($product_id)) {
die("Error. Product Doesn't Exist");
}
switch($action) {
case "add":
$_SESSION['cart'][$product_id]++;
break;
case "remove":
$_SESSION['cart'][$product_id]--;
if($_SESSION['cart'][$product_id] == 0) unset($_SESSION['cart'][$product_id]);
break;
case "empty":
unset($_SESSION['cart']);
break;
}
?>
但是在将第一个商品添加到购物车期间出现了错误。我该如何纠正它。
错误:
注意:未定义索引:第 16 行 C:\wamp\www\website\store_esp\addtocart.php 中的购物车
注意:第 16 行 C:\wamp\www\website\store_esp\addtocart.php 中未定义索引:2
I was trying to make a shopping cart and got a code from web..
<?php
session_start();
require_once 'class/Item.php';
$product_id = $_REQUEST['i_id'];
$action = $_REQUEST['action'];
$item= new Item();
if($product_id && !$item->productExists($product_id)) {
die("Error. Product Doesn't Exist");
}
switch($action) {
case "add":
$_SESSION['cart'][$product_id]++;
break;
case "remove":
$_SESSION['cart'][$product_id]--;
if($_SESSION['cart'][$product_id] == 0) unset($_SESSION['cart'][$product_id]);
break;
case "empty":
unset($_SESSION['cart']);
break;
}
?>
but durring adding the first item to the cart it goes error. How can I correct it.
Error:
Notice: Undefined index: cart in C:\wamp\www\website\store_esp\addtocart.php on line 16
Notice: Undefined index: 2 in C:\wamp\www\website\store_esp\addtocart.php on line 16
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看起来您可能正在尝试操作尚未设置的变量。在执行操作之前,请确保检查
$_SESSION['cart'][$product_id]
是否存在:Looks like you may be trying to manipulate variables that aren't setup yet. Make sure you're checking that
$_SESSION['cart'][$product_id]
exists before you do something on it:尝试将其更改
为:
Try changing this:
to this:
如果不知道错误,就不可能确定。但用我的演绎能力,我认为问题出在这里:
应该是这样的:
你需要改变这个:
Without knowing the error, it's impossible to tell for sure. But using my deductive powers, I think the problem is here:
It should be this:
And you need to change this: