PHP 会话错误

发布于 2024-10-07 15:04:34 字数 854 浏览 14 评论 0原文

我试图制作一个购物车并从网络上获取了一个代码..

<?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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

裸钻 2024-10-14 15:04:34

看起来您可能正在尝试操作尚未设置的变量。在执行操作之前,请确保检查 $_SESSION['cart'][$product_id] 是否存在:

if(!isset($_SESSION['cart'][$product_id]))
  $_SESSION['cart'][$product_id] = 0;

switch($action) {
...

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:

if(!isset($_SESSION['cart'][$product_id]))
  $_SESSION['cart'][$product_id] = 0;

switch($action) {
...
等风来 2024-10-14 15:04:34

尝试将其更改

$_SESSION['cart'][$product_id]++;

为:

if (isset($_SESSION['cart'][$product_id])) {
    ++$_SESSION['cart'][$product_id];
} else {
    $_SESSION['cart'][$product_id] = 1;
}

Try changing this:

$_SESSION['cart'][$product_id]++;

to this:

if (isset($_SESSION['cart'][$product_id])) {
    ++$_SESSION['cart'][$product_id];
} else {
    $_SESSION['cart'][$product_id] = 1;
}
只是在用心讲痛 2024-10-14 15:04:34

如果不知道错误,就不可能确定。但用我的演绎能力,我认为问题出在这里:

$_SESSION['cart'][$product_id]++;

应该是这样的:

if (isset($_SESSION['cart'][$product_id])) {
    $_SESSION['cart'][$product_id]++;
} else {
    $_SESSION['cart'][$product_id] = 1;
}

你需要改变这个:

session_start();
// add this part
if (!isset($_SESSION['cart'])) {
    $_SESSION['cart'] = array();
}
require_once 'class/Item.php';
$product_id = $_REQUEST['i_id'];
$action = $_REQUEST['action']; 

Without knowing the error, it's impossible to tell for sure. But using my deductive powers, I think the problem is here:

$_SESSION['cart'][$product_id]++;

It should be this:

if (isset($_SESSION['cart'][$product_id])) {
    $_SESSION['cart'][$product_id]++;
} else {
    $_SESSION['cart'][$product_id] = 1;
}

And you need to change this:

session_start();
// add this part
if (!isset($_SESSION['cart'])) {
    $_SESSION['cart'] = array();
}
require_once 'class/Item.php';
$product_id = $_REQUEST['i_id'];
$action = $_REQUEST['action']; 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文