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

CodeIgniter购物车多商品存入MySQL表仅第一条保存问题求助

逻辑问题定位

  • 遍历购物车的foreach循环仅重复给$itemname、$productid两个变量赋值,每次循环都会覆盖上一次的变量值,循环结束后这两个变量仅保留购物车最后一个商品的信息,自然无法拿到全部商品数据。
  • 订单写入方法addorder()仅被调用了一次,就算你提前拿到了全部商品数据,也只会写入一条商品记录到数据库。
  • 现有表结构设计存在冗余问题,把用户公共信息和单商品信息存在同一张表,同一个订单的用户信息会随商品数量重复存储N次,后期维护成本极高。

修复方案

临时修复(不修改现有表结构)

把购物车遍历逻辑放到提交判断内部,循环写入每个商品的订单记录即可:

public function checkout()            
{
    $data['items'] = array_values(unserialize($this->session->userdata('cart')));
    $data['total'] = $this->total();
     
    if(isset($_POST['confirm']))
    {
        $name = $this->input->post('name');
        $phone = $this->input->post('phone');
        $email = $this->input->post('email');
        $address = $this->input->post('address');
        $total = $this->input->post('total');
        // 遍历逻辑移到提交判断内,循环写入每个商品
        foreach ($data['items'] as $item){
            $itemname = $item['name'];
            $productid = $item['id'];
            $this->product->addorder($name,$phone,$email,$address,$itemname,$total,$productid);
        }
        // 写入完成后建议加跳转,避免用户刷新页面重复提交
        redirect('order/success');
    }

    $this->load->view('checkout', $data);
}

最优方案(调整表结构,避免数据冗余)

推荐拆分订单主表和订单明细表,消除重复数据:

  1. 新增两张业务表:
    • 订单主表orders:字段包含order_id(主键自增)、name、phone、email、address、total、create_time,存储订单公共信息
    • 订单明细表order_items:字段包含item_id(主键自增)、order_id(关联订单主表ID)、product_id、product_name,存储单商品信息
  2. 模型新增对应写入方法:
// 写入订单主表,返回生成的自增订单ID
public function add_order_main($name,$phone,$email,$address,$total)
{
    $data = [
        'name' => $name,
        'phone' => $phone,
        'email' => $email,
        'address' => $address,
        'total' => $total,
        'create_time' => date('Y-m-d H:i:s')
    ];
    $this->db->insert('orders', $data);
    return $this->db->insert_id();
}

// 写入单条订单商品明细
public function add_order_item($order_id, $product_id, $product_name)
{
    $data = [
        'order_id' => $order_id,
        'product_id' => $product_id,
        'product_name' => $product_name
    ];
    return $this->db->insert('order_items', $data);
}
  1. 调整控制器逻辑:
public function checkout()            
{
    $data['items'] = array_values(unserialize($this->session->userdata('cart')));
    $data['total'] = $this->total();
     
    if(isset($_POST['confirm']))
    {
        $name = $this->input->post('name');
        $phone = $this->input->post('phone');
        $email = $this->input->post('email');
        $address = $this->input->post('address');
        $total = $this->input->post('total');
        // 先写主表拿到订单ID
        $order_id = $this->product->add_order_main($name,$phone,$email,$address,$total);
        // 循环写入所有商品明细
        foreach ($data['items'] as $item){
            $this->product->add_order_item($order_id, $item['id'], $item['name']);
        }
        redirect('order/success');
    }

    $this->load->view('checkout', $data);
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.01 02:57:03