添加产品到购物车

时间:2015-03-03 23:10:13

标签: php mysql shopping-cart

目标:

  • 按添加到购物车按钮,该按钮链接到名为$prod_id
  • 的产品ID
  • 将产品ID,数量和价格存储到$_SESSION['cart']
  • 显示存储在$_SESSION
  • 中的项目

问题:

  • 按下添加到购物车后,购物车显示空表
  • $_SESSION未正确存储来自MySQL数据库产品的信息

我的添加到购物车功能的代码

    <?php


// If User Pushes Add Cart Button --------//
function cart(){

    global $dbc;
    if(isset($_GET['add_cart'])){

        $prod_id = $_GET['add_cart'];  // gets the product id from add_cart link extension

        // Check to see if product is already in the session

        if(isset($_SESSION['cart'][$prod_id])) { // if prod id is already in the session

            $_SESSION['cart'][$prod_id]['quantity']++; //increase the qty of the product

        } else { //if product is NOT in the session

        //check the PRODUCTS DB for the product id
        $sql = mysqli_query($dbc,"SELECT * FROM products WHERE id_product = '$prod_id'") or die (mysqli_error($dbc));

                if(mysqli_num_rows($sql) != 0 ){ //if the results of this search is NOT empty

                    while ($row = mysqli_fetch_array($sql)){;//get each row

                    //store array $row into the SESSION array
                    $_SESSION["cart"][$row['id_product']]= array(
                            "quantity" => 1,
                            "price" => $row['price']);
                    }

                }else{

                $message = "The product could not be added.";
                }}}}
?>

0 个答案:

没有答案