You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP HTML购物车实现移除功能后首个商品无法显示问题求助

购物车移除商品后首个商品无法显示的问题排查

问题描述

我开发了一个支持用户下单的PHP+HTML购物车,商品添加功能原本运行正常,但实现移除按钮后,购物车中的首个商品无法显示,仅第二个和第三个商品能正常展示。请帮忙排查问题所在。

相关代码如下:

//添加商品功能// 
if(isset($_POST["add"])){                                                    
  if (isset($_SESSION["cart"]))
      $item_array_id = array_column($_SESSION["cart"], "SKU");
     
      if(!in_array($_GET["SKU"], $item_array_id)){
          $count = count($_SESSION["cart"]);
          
          $item_array = array(
             'SKU' => $_GET["SKU"], 
             'item_name' => $_POST["hidden_name"], 
             'quantity' => $_POST["quantity"],
             'item_description' => $_POST["hidden_description"]
          ); 

          $_SESSION["cart"][$count] = $item_array; 
          echo '<script> window.location="lijstweergave.php"</script>'; 
      }
       else{
        echo '<script>alert("Product is already added to cart")</script>'; 
        echo '<script>window.location="lijstweergave.php"</script>';  
      }
    } else{
      $item_array = array(    'SKU' => $_GET["SKU"], 
                              'item_name' => $_POST["hidden_name"], 
                              'quantity' => $_POST["quantity"],
                              'item_description' => $_POST["hidden_description"]
                            ); 

    $_SESSION["cart"][0] = $item_array;   
    }    
}


//移除商品部分 ////此处是否存在问题?// 
if (isset($_POST['remove_item'])) {
    $remove_SKU = $_POST['remove_SKU'];
    foreach ($_SESSION['cart'] as $key => $value) {
        if ($value['SKU'] == $remove_SKU) {
            unset($_SESSION['cart'][$key]);
            $_SESSION['cart'] = array_values($_SESSION['cart']);
            break;
        }
    }
}

问题根源与修复方案

核心问题:添加商品的条件判断缺少花括号

你的问题出在添加商品功能的第一个if (isset($_SESSION["cart"]))语句未包裹花括号,导致逻辑执行顺序混乱,破坏了购物车数组的结构,最终引发首个商品无法显示的问题。

具体分析

原代码中,if (isset($_SESSION["cart"]))没有加{},它只会执行紧跟的$item_array_id = array_column($_SESSION["cart"], "SKU");这一行代码,而后续的if(!in_array($_GET["SKU"], $item_array_id))会无条件执行——哪怕$_SESSION["cart"]还未初始化(首次添加商品时)。

这种情况下,array_column会因传入未定义的数组报错,且in_array检查的是一个无效的$item_array_id,直接导致逻辑走偏,购物车数组的索引或内容出现异常,后续移除操作后就暴露了首个商品无法显示的问题。

修复代码

给if (isset($_SESSION["cart"]))添加花括号,包裹所有属于该条件的代码块:

//添加商品功能// 
if(isset($_POST["add"])){                                                    
  if (isset($_SESSION["cart"])) { // 新增左花括号
      $item_array_id = array_column($_SESSION["cart"], "SKU");
     
      if(!in_array($_GET["SKU"], $item_array_id)){
          $count = count($_SESSION["cart"]);
          
          $item_array = array(
             'SKU' => $_GET["SKU"], 
             'item_name' => $_POST["hidden_name"], 
             'quantity' => $_POST["quantity"],
             'item_description' => $_POST["hidden_description"]
          ); 

          $_SESSION["cart"][$count] = $item_array; 
          echo '<script> window.location="lijstweergave.php"</script>'; 
      } else {
        echo '<script>alert("Product is already added to cart")</script>'; 
        echo '<script>window.location="lijstweergave.php"</script>';  
      }
  } else { // 新增左花括号
      $item_array = array(    
          'SKU' => $_GET["SKU"], 
          'item_name' => $_POST["hidden_name"], 
          'quantity' => $_POST["quantity"],
          'item_description' => $_POST["hidden_description"]
      ); 

      $_SESSION["cart"][0] = $item_array;   
  }    
}

移除商品代码的说明

你标注疑问的移除商品逻辑是正常的:通过SKU匹配目标项,unset后用array_values重新索引数组,避免出现断键问题,这部分代码没有错误。


内容的提问来源于stack exchange,提问作者Erik

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 12:55:26