简体   繁体   English

我无法在会话中更新产品数量

[英]I can't update the product quantity in session

I'm trying to make a shopping cart with PHP.我正在尝试用 PHP 制作购物车。

I can add products to the database, but while I was testing, I have discovered a bug.我可以将产品添加到数据库中,但是在我进行测试时,我发现了一个错误。 Quantity of pieces does not change if I add the same one in addition to a previously added product.如果除了先前添加的产品之外添加相同的产品,件数量不会改变。 I just want to increase the amount when it is added again from a previously added product.我只想在从以前添加的产品中再次添加时增加数量。 But I tried two different ways, but I failed.但是我尝试了两种不同的方法,但都失败了。 I may be making a mistake.我可能犯了一个错误。

the point we need to focus on in the code : after comment line => "//if item already added to cart. Change quantity in shopping card"我们需要在代码中关注的一点:在注释行之后 => "//如果商品已添加到购物车。更改购物卡中的数量"
My Shopping card ss and var_dump["Session"] is here.我的购物卡 ss 和 var_dump["Session"] 在这里。
My Codes is here我的代码在这里

The first method I try is:我尝试的第一种方法是:

//Check if cart is not empty.
if(isset($_SESSION["cart"]))
{
    $item_array_id = array_column($_SESSION["cart"], "product_id");

    //Check if the same items not added to cart.
    if(!in_array($_GET["id"], $item_array_id))
    {

        $item_array = array(
            'product_id' => $_GET["id"],
            'item_name' => $_POST["hidden_name"],
            'product_price' => $_POST["hidden_price"],
            'item_quantity' => $_POST["quantity"]
        );
        $_SESSION["cart"][$count] = $item_array;
        echo '<script>alert("Ürün sepete eklendi.")</script>';
        echo '<script>window.location="product.php"</script>';
    }

    //If item already added to cart change quantity in shopping cart
    else
    {   
        $cnt=0;
        foreach($_SESSION["cart"] as $keys => $values){

            if($values["product_id"]==$_GET["id"]){
                $yeni= $values["item_quantity"]+$_POST["quantity"];
                echo $yeni;
                $item_array = array(
                    'product_id' => $_GET["id"],
                    'item_name' => $_POST["hidden_name"],
                    'product_price' => $_POST["hidden_price"],
                    'item_quantity' => $_POST["quantity"]
                );
                $_SESSION["cart"][$cnt] = $item_array;
            }
            $cnt++;
        }

        echo '<script>alert("Product updated in shoping cart")</script>';
        echo '<script>window.location="product.php"</script>';


    }
}

//if cart is empty. Create SESSION["cart"]
else
{
    $item_array = array(
        'product_id' => $_GET["id"],
        'item_name' => $_POST["hidden_name"],
        'product_price' => $_POST["hidden_price"],
        'item_quantity' => $_POST["quantity"]
    );
    $_SESSION["cart"][0] = $item_array;
    echo '<script>alert("Ürün sepetinize eklendi.")</script>';
    echo '<script>window.location="product.php"</script>';
}

The second method I try is:我尝试的第二种方法是:

   //Check if cart is not empty.
  if(isset($_SESSION["cart"]))
  {
    $item_array_id = array_column($_SESSION["cart"], "product_id");

    //Check if the same items not added to cart.
    if(!in_array($_GET["id"], $item_array_id))
    {

        $item_array = array(
        'product_id' => $_GET["id"],
        'item_name' => $_POST["hidden_name"],
        'product_price' => $_POST["hidden_price"],
        'item_quantity' => $_POST["quantity"]
        );
        $_SESSION["cart"][$count] = $item_array;
        echo '<script>alert("Ürün sepete eklendi.")</script>';
        echo '<script>window.location="product.php"</script>';
    }

    //If item already added to cart change quantity in shopping cart
    else
    {   
        $cnt=0;
        foreach($_SESSION["cart"] as $keys => $values){

            if($values["product_id"]==$_GET["id"]){

                $_SESSION["cart"][$cnt][$values]=$values["item_quantity"]+$_POST["quantity"];
            }
            $cnt++;
        }

        echo '<script>alert("Ürün güncellendi.")</script>';
        echo '<script>window.location="product.php"</script>';
    }
}

//If cart is empty create SESSION["cart"]
else
{
    $item_array = array(
    'product_id' => $_GET["id"],
    'item_name' => $_POST["hidden_name"],
    'product_price' => $_POST["hidden_price"],
    'item_quantity' => $_POST["quantity"]
    );
    $_SESSION["cart"][0] = $item_array;
    echo '<script>alert("Ürün sepetinize eklendi.")</script>';
    echo '<script>window.location="product.php"</script>';
}

Both methods do not change the amount of the product available.这两种方法都不会改变可用产品的数量。 can you help me with this ?你能帮我解决这个问题吗?

尝试这个:

$_SESSION["cart"][$cnt]['item_quantity'] += $_POST['quantity']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM