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

时间:2015-07-22 10:42:20

标签: php shopping-cart cart

以下代码有什么问题?

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 

}

当我添加product_color时,产品未添加。编写数组语法或检查购物车中存在的product_codeproduct_color时出了什么问题?

1 个答案:

答案 0 :(得分:0)

您必须在索引页面中将product_color分配给隐藏变量。然后,只有您可以添加$new_product["product_color"]=$_POST["product_color"];

实施例

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