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

PHP购物车开发报错:Invalid argument supplied for foreach()及数据读取失败

解决"Invalid argument supplied for foreach()"及数据库读取问题

让我们一步步拆解你的问题:

核心错误原因

1. 遍历方式错误

mysqli_fetch_assoc($result)返回的是单条记录的一维关联数组(如果查询有结果),或者false(如果无结果)。你现在用foreach($row as $i)遍历的话,$i会是每个字段的具体值(比如字符串、数字),而不是一个数组,所以$i['ord_id']这种写法肯定会报错。而且如果查询无结果,$row是false,foreach遍历布尔值就会触发你看到的"Invalid argument supplied for foreach()"错误。

2. $last_id取值逻辑错误

mysqli_insert_id($con)的作用是获取最近一次INSERT操作生成的自增ID,但你刚连接数据库,还没有执行任何INSERT语句,所以这个值会是0。你的SQL查询SELECT * FROM product_order WHERE ord_id = 0大概率找不到任何记录,导致$result是空集,$row为false,这直接导致了foreach的错误。

修正后的代码方案

场景1:你确实要查询单条记录(比如刚插入的订单)

先确保你是在执行INSERT订单的操作之后再获取$last_id,然后直接使用$row的字段,不需要foreach:

<?php 
include('../Neo/assets/session.inc.php'); 
include 'config/config.php'; 

if (!$con) { 
    die("Connection failed: " . mysqli_connect_error()); 
}

// 注意:这里必须先执行INSERT product_order的语句,比如:
// $insert_sql = "INSERT INTO product_order (card_id, ord_qty, ...) VALUES (...)";
// mysqli_query($con, $insert_sql);

// 只有执行完INSERT后,才能正确获取last_id
$last_id = mysqli_insert_id($con); 
$_SESSION['last_id'] = $last_id; 

$sql = "SELECT * FROM product_order WHERE ord_id = " . $last_id; 
$result = mysqli_query($con,$sql); 

// 先判断查询是否有结果
if($result && mysqli_num_rows($result) > 0){
    $row = mysqli_fetch_assoc($result); 
    // 直接输出单条记录的字段
    echo '<tr> 
            <td> '.$row['ord_id'].'</td> 
            <td> '.$row['card_id'].'</td> 
            <td> '.$row['field1'].'</td> 
            <td> '.$row['field2'].'</td> 
            <td> '.$row['field3'].'</td> 
            <td> '.$row['field4'].'</td> 
            <td> '.$row['field5'].'</td> 
            <td> '.$row['ord_qty'].'</td> 
            <td> '.$row['card_price'].'</td> 
            <td> '.$row['ord_total'].'</td> 
          </tr>';
} else {
    echo '<tr><td colspan="10">没有找到对应的订单记录</td></tr>';
}
?>

场景2:你要查询多条订单记录(比如购物车的多个商品)

如果是要遍历多条记录,需要循环获取结果,或者用mysqli_fetch_all:

<?php 
include('../Neo/assets/session.inc.php'); 
include 'config/config.php'; 

if (!$con) { 
    die("Connection failed: " . mysqli_connect_error()); 
}

// 这里根据实际需求调整查询条件,比如查询当前用户的所有订单
$sql = "SELECT * FROM product_order WHERE user_id = " . $_SESSION['user_id']; 
$result = mysqli_query($con,$sql); 

// 判断查询是否成功且有结果
if($result && mysqli_num_rows($result) > 0){
    // 循环遍历每条记录
    while($row = mysqli_fetch_assoc($result)){
        echo '<tr> 
                <td> '.$row['ord_id'].'</td> 
                <td> '.$row['card_id'].'</td> 
                <td> '.$row['field1'].'</td> 
                <td> '.$row['field2'].'</td> 
                <td> '.$row['field3'].'</td> 
                <td> '.$row['field4'].'</td> 
                <td> '.$row['field5'].'</td> 
                <td> '.$row['ord_qty'].'</td> 
                <td> '.$row['card_price'].'</td> 
                <td> '.$row['ord_total'].'</td> 
              </tr>';
    }
} else {
    echo '<tr><td colspan="10">购物车为空</td></tr>';
}
?>

额外建议

  • 永远要对数据库查询的结果做判断,避免因为无结果导致的遍历错误。
  • 为了安全,建议使用预处理语句来防止SQL注入,比如:
$stmt = mysqli_prepare($con, "SELECT * FROM product_order WHERE ord_id = ?");
mysqli_stmt_bind_param($stmt, "i", $last_id);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 07:45:35