简体   繁体   English

向购物车添加产品时出现问题

[英]issues while adding product to shopping cart

what's wrong in below code? 下面的代码有什么问题?

if(isset($_POST["product_code"]) && isset($_POST["product_color"]))
{
    foreach($_POST as $key => $value){
        $new_product[$key] = filter_var($value, FILTER_SANITIZE_STRING); //create a new product array 
    }

    //we need to get product name and price from database.
    $statement = $mysqli_conn->prepare("SELECT product_name, product_price FROM products_list WHERE product_code=? LIMIT 1");
    $statement->bind_param('s', $new_product['product_code']);
    $statement->execute();
    $statement->bind_result($product_name, $product_price);


    while($statement->fetch()){ 
        $new_product["product_name"] = $product_name; //fetch product name from database
        $new_product["product_price"] = $product_price;  //fetch product price from database
        $new_product["product_color"] = $_POST["product_color"];

        if(isset($_SESSION["products"])){  //if session var already exist
        $check = $_SESSION["products"][$new_product['product_code']][$new_product['product_color']];
            if(isset($check)) //check item exist in products array
            {
                unset($check); //unset old item
            }           
        }

        $check = $new_product;  //update products with new item array   
    }

    $total_items = count($_SESSION["products"]); //count total items
    die(json_encode(array('items'=>$total_items))); //output json 

}

when I have added product_color then product is not getting added. 当我添加product_color就不会添加产品。 What's wrong while writing the array syntax or checking the product_code and product_color present in cart or not? 编写数组语法或检查购物车中是否存在product_codeproduct_color出了什么问题?

You have to assign the product_color to hidden variable in index page. 您必须将product_color分配给索引页中的隐藏变量。 Then only you can added in $new_product["product_color"]=$_POST["product_color"]; 然后只有您可以添加$new_product["product_color"]=$_POST["product_color"];

Example

<input type="hidden" name="product_color" value=<?php Echo $obj->product_color;?> />

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

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