doucheng3811 2018-03-06 07:25
浏览 63
已采纳

无法将相同的产品添加到购物车

I have created an add to cart code using session and jquery. It's almost done but I am getting two issues.

1) When I refresh the page, I am getting error

Notice: Undefined index: quantity in C:\xampp\htdocs\add-to-cart\ajax_cart.php on line 5

2) I am able to add the product to the cart but If I add the same product two times then I am getting the error.

 Notice: Undefined index: 250.00 in C:\xampp\htdocs\add-to-cart\ajax_cart.php on line 30
 Notice: Undefined offset: 1 in C:\xampp\htdocs\add-to-cart\ajax_cart.php on line 31

There is some issue with else part (might be the price).

else{
    $_SESSION['product_cart'][$product_id][$product_price] = $_SESSION['product_cart'][$product_id][$product_price] + ($product_price*$quantity);
    $_SESSION['product_cart'][$product_id][$quantity] = $_SESSION['product_cart'][$product_id][$quantity]+$quantity;
    }

AJAX function to call the ajax_cart.php

function add_cart(p_id=""){
    var quantity = $(".quantity"+p_id).val();
    $.ajax({
        type:"post",
        url:"ajax_cart.php",
        data:{action:'add',p_id:p_id,quantity:quantity},
        success:function(result){
            $('.cart_data').html(result);
        }
    });
}

Add to Cart

$action = $_POST['action'];
 $p_id   = trim($_POST['p_id']);
 $quantity = $_POST['quantity'];
if($action == 'add'){
     if(!empty($p_id)){
           $query = "SELECT product_id,product_title,product_price,product_image FROM product WHERE product_id=?";
            if ($stmt = $conn->prepare($query)) {
                $stmt->bind_param("i", $p_id);
                $stmt->execute();
                $stmt->bind_result($product_id, $product_title,$product_price, $product_image);
                $stmt->fetch();
              }

             $product = array(
                "p_id"=>$product_id,
                "title"=>$product_title,
                "price"=>$product_price*$quantity,
                "image"=>$product_image,
                "quantity"=>$quantity
             );
        if(isset($_SESSION['product_cart']) && !empty($_SESSION['product_cart']))
        {
            if(!array_key_exists($product_id,$_SESSION['product_cart']))
            {
                $_SESSION['product_cart'][$product_id] = $product;
            }
            else{
                $_SESSION['product_cart'][$product_id][$product_price] = $_SESSION['product_cart'][$product_id][$product_price] + ($product_price*$quantity);
                $_SESSION['product_cart'][$product_id][$quantity] = $_SESSION['product_cart'][$product_id][$quantity]+$quantity;
            }       
        }
        else{
          $_SESSION['product_cart'][$product_id] = $product;
        }
    }   
}
  • 写回答

2条回答 默认 最新

  • dongpo2340 2018-03-06 07:31
    关注

    use the else part as below

    else{
                $_SESSION['product_cart'][$product_id]['price'] += ($product_price*$quantity);
                $_SESSION['product_cart'][$product_id]['quantity'] += $quantity;
            }   
    

    and add below code

    if(!isset($_POST['action']) || !isset($_POST['quantity'])) return;

    above the line

    $action = $_POST['action'];

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒