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

时间:2018-06-20 11:37:08

标签: php session

我正在尝试用PHP制作购物车。

我可以将产品添加到数据库中,但是在进行测试时,我发现了一个错误。如果除了先前添加的产品之外添加相同的产品,件数也不会改变。我只是想增加从先前添加的产品中再次添加的数量。但是我尝试了两种不同的方法,但是失败了。我可能做错了。

我们需要在代码中重点关注的地方:在注释行之后=>“ //如果商品已添加到购物车。请更改购物卡中的数量”
My Shopping card ss and var_dump["Session"] is here.
My Codes is here

我尝试的第一种方法是:

//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. Chanhe quantity in shopping card
    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 shooping card")</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>';
}

我尝试的第二种方法是:

   //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. Chanhe quantity in shopping card
    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>';
}

两种方法都不会改变可用产品的数量。你能帮我吗?

1 个答案:

答案 0 :(得分:0)

尝试一下:

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