【问题标题】:Update $_SESSION product quantity not updating更新 $_SESSION 产品数量未更新
【发布时间】:2020-03-09 23:50:29
【问题描述】:

我正在尝试更新购物车中的产品,但它不起作用..我不知道问题是什么。

<div class="quantity">
  <input class="input_display" name="aantal" type="number" value="<?=$value['item_quantity'];?>" min="1" max="<?php if($current_max_input_value == 0){ echo $max_input_number; }else{ echo $current_max_input_value; };?>">
</div>
<a href="?action=update&id=<?=$value['item_id'];?>" class="site-btn">Update Item</a>
if(isset($_GET['action'])){
    if($_GET['action'] == 'update'){
        foreach($_SESSION['shopping_cart'] as $key => $item){
            //echo '<pre>';
            //print_r($_SESSION['shopping_cart']);
            //echo '<pre>';
            if($item['item_id'] == $_POST['id']){

                //UPDATE THE ITEM IN SHOPPING CART
                $_SESSION['shopping_cart'][$key]['item_quantity'] = $_POST['aantal'];
            }

        }
    }
}

【问题讨论】:

    标签: php session shopping-cart


    【解决方案1】:

    这里的问题是,当您使用执行 GET 调用的&lt;a&gt; 时,您不会传递 POST 变量。相反,请尝试使用 POST vars 对表单中的按钮 Update Item 和字段“项目数”进行分组。现在您只能通过 PHP 脚本中的 POST vars 获取值,将 $_GET 替换为 $_POST。

    <form action="cart.php" method="post">
          <input type="hidden" name="action" value="update">
          <input type="hidden" name="id" value="<?=$value['item_id'];?>">
          <div class="quantity">
            <input class="input_display" name="aantal" type="number" value="<?=$value['item_quantity'];?>" min="1" max="<?php if($current_max_input_value == 0){ echo $max_input_number; }else{ echo $current_max_input_value; };?>">
          </div>
          <button type="submit" class="site-btn">Update Item</a>
        </form>
    
    if(isset($_POST['action'])){
        if($_POST['action'] == 'update'){
            foreach($_SESSION['shopping_cart'] as $key => $item){
                //echo '<pre>';
                //print_r($_SESSION['shopping_cart']);
                //echo '<pre>';
                if($item['item_id'] == $_POST['id']){
    
                    //UPDATE THE ITEM IN SHOPPING CART
                    $_SESSION['shopping_cart'][$key]['item_quantity'] = $_POST['aantal'];
                }
    
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-19
      • 2021-09-29
      • 2014-08-30
      • 2023-01-29
      • 2015-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多