【问题标题】:PHP / Sessions Shopping Cart - Quantity in cart only updates for first itemPHP / Sessions 购物车 - 购物车中的数量仅更新第一项
【发布时间】:2017-11-30 15:36:33
【问题描述】:

一直在修改我的添加到购物车功能的代码,如果产品已经存在于购物车中,我在不更新数量方面遇到了一些困难。 它只会为添加到购物车的第一个产品正确更新。

我的更新添加到购物车代码是:

// -------------------------    
// Adding item
// -------------------------    
case "add":
    if(!empty($_POST["quantity"])) {
        $productByCode = $db_handle->runQuery("SELECT * FROM products WHERE product_id='" . $_GET["product_id"] . "'");
        $itemArray = array($productByCode[0]["product_id"]=>array('product_name'=>$productByCode[0]["product_name"], 'product_description'=>$productByCode[0]["product_description"],
        'product_id'=>$productByCode[0]["product_id"], 'quantity'=>$_POST["quantity"], 'product_price'=>$productByCode[0]["product_price"],'image'=>$productByCode[0]["image"]));

        if(!empty($_SESSION["cart_item"])) {
            if(in_array($productByCode[0]["product_id"], array_keys($_SESSION["cart_item"]))) {
                foreach($_SESSION["cart_item"] as $k => $v) {
                        if($productByCode[0]["product_id"] == $k) {
                            if(empty($_SESSION["cart_item"][$k]["quantity"])) {
                                $_SESSION["cart_item"][$k]["quantity"] = 0;
                            }
                            $_SESSION["cart_item"][$k]["quantity"] += $_POST["quantity"];
                        }
                }
            } else {
                $_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],$itemArray);
            }
        } else {
            $_SESSION["cart_item"] = $itemArray;
        }
    }
break;

我在代码中遗漏了什么吗?

谢谢。 斯坦。

编辑: 更新的代码不起作用:

    // -------------------------    
// Adding item
// -------------------------    
case "add":
    if(!empty($_POST["quantity"])) {
        $productByCode = $db_handle->runQuery("SELECT * FROM products WHERE product_id='" . $_GET["product_id"] . "'");
        $itemArray = array($productByCode[0]["product_id"]=>array('product_name'=>$productByCode[0]["product_name"], 'product_description'=>$productByCode[0]["product_description"],
        'product_id'=>$productByCode[0]["product_id"], 'quantity'=>$_POST["quantity"], 'product_price'=>$productByCode[0]["product_price"],'image'=>$productByCode[0]["image"]));

        if(!empty($_SESSION["cart_item"])) {
            if(in_array($productByCode[0]["product_id"], array_keys($_SESSION["cart_item"]))) {
                foreach($_SESSION["cart_item"] as $k => $v) {
                        if($productByCode[0]["product_id"] == $k) {
                            if(empty($_SESSION["cart_item"][$k]["quantity"])) {

                                **foreach($_SESSION["cart_item"] as $k =>&$v){

                                    $_SESSION["cart_item"][$k]["quantity"] += $_POST["quantity"];   

                                }**

                            }
                            $_SESSION["cart_item"][$k]["quantity"] += $_POST["quantity"];
                        }
                }
            } else {
                $_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],$itemArray);
            }
        } else {
            $_SESSION["cart_item"] = $itemArray;
        }
    }
break;

【问题讨论】:

  • 您的代码容易受到SQL injection 攻击。您应该通过mysqliPDO 驱动程序使用带有绑定参数的预处理语句。 This post 有一些很好的例子。
  • 既然您现在已经发布了您的实时 URL 和有关您的数据库结构的信息,您将希望立即将此离线。网络上的任何人现在都可以清除您的数据库。
  • 链接到演示并不是在这里获得帮助的最佳方法。请查看SO's how to create a Minimal, Verifiable example。请包含您的表单的 html。

标签: php session shopping-cart


【解决方案1】:

穿上火焰套装:

我相信你这样做的时候

$_SESSION["cart_item"][$v]["quantity"] = $v++;

PHP 实际上创建了您正在迭代的数组的副本

既然你想写入这个数组,你应该使用&通过引用显式迭代

foreach($_SESSION["cart_item"] as $k =>&$v)...

【讨论】:

  • 抱歉,我更改了该行以测试输出。我现在已经用有效的代码更新了原始帖子。
  • 基本前提仍然存在,一旦您尝试写入数组,您必须通过引用而不是值来完成
  • 编辑了主要帖子以显示与上述代码一起使用的代码,但仍然没有运气。我不明白为什么它适用于第一个产品,但之后就没有了。
  • 还有其他人可以提供任何帮助吗?
【解决方案2】:

试试这个 这里我在会话中存储数据 $pr_id = 产品 ID

adding item to cart
    if(isset($_SESSION['cart_items']))
        {
            //find index of an array if it exits then do not add else add to cart and then
            if (!array_key_exists($pr_id,$_SESSION['cart_items']))
            {
                $_SESSION['cart_items'][$pr_id]['id'] = $pr_id;
                $_SESSION['cart_items'][$pr_id]['quantity'] = $quantity;
            }
        }
        else
        {
            $_SESSION['cart_items'][$pr_id]['id'] = $pr_id;
            $_SESSION['cart_items'][$pr_id]['quantity'] = $quantity;        
        }
        $arr = array();
        $arr['count'] = count($_SESSION['cart_items']);
        $arr['message'] = 'Item successFully added';
        echo json_encode($arr);

这是工作代码,您可以在 db 中进行更新条目

用于更新产品的条目或数量

if (array_key_exists($pr_id,$_SESSION['cart_items']))
    {
        $_SESSION['cart_items'][$pr_id]['quantity']=$quantity;
    }
    $arr['count'] = count($_SESSION['cart_items']);
    $arr['message'] = 'Quantity changed successfully. ';
    $arr['status']  = true;
    echo json_encode($arr);

【讨论】:

  • 感谢您,我真的不想更改所有代码。我一定错过了一些非常简单的东西。
【解决方案3】:

我犯了和你一样的错误。 我就是这样解决的:

不要使用product_idproduct_id 是一个数字。 使用由数字和字母组合而成的东西,例如 product_name。 它对我有用。

$productByCode = $db_handle->runQuery("SELECT * FROM products WHERE product_id='" . $_GET["product_name"] . "'");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-17
    • 1970-01-01
    • 1970-01-01
    • 2021-04-19
    • 1970-01-01
    • 2015-06-01
    相关资源
    最近更新 更多