【问题标题】:issues while adding product to shopping cart将产品添加到购物车时出现问题
【发布时间】:2015-07-22 10:42:19
【问题描述】:

下面的代码有什么问题?

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 时出了什么问题?

【问题讨论】:

    标签: php shopping-cart cart


    【解决方案1】:

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

    例子

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

    【讨论】:

      猜你喜欢
      • 2011-09-23
      • 1970-01-01
      • 2022-10-01
      • 2012-06-09
      • 1970-01-01
      • 2011-12-19
      • 2014-11-21
      • 1970-01-01
      相关资源
      最近更新 更多