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

如何在CodeIgniter中清空指定MySQL表并重新插入数据

问题描述

现有代码实现从CSV文件导入数据到多个MySQL表,逻辑为:表为空时执行插入,表中有数据则执行更新。需求是指定表gap_up每次导入数据前先清空,但修改代码后仅清空了表内容,新数据未成功插入。

原代码

$records = $this->csvimport->parse_csv($this->directory . $file);

foreach ($records as $row) {
    $data = $this->$function($row);
    
    $data = $this->correct_date_format($data);
    
    foreach ($this->$condition($data) as $column) {
        $this->db->where($column, $data[$column]);
    }

    if ($this->db->get($table, 1)->num_rows() > 0) {
        foreach ($this->$condition($data) as $column) {
            $this->db->where($column, $data[$column]);
        }

        $this->db->update($table, $data);

    } else {
        $this->db->insert($table, $data);
    }
}
echo json_encode(['Last_record' => $this->db->where('date', $data['date'])->get($table)->result_array()]);

修改后的代码(存在问题)

$records = $this->csvimport->parse_csv($this->directory . $file);

foreach ($records as $row) {
    $data = $this->$function($row);
    
    $data = $this->correct_date_format($data);
    
    foreach ($this->$condition($data) as $column) {
        $this->db->where($column, $data[$column]);
    }

    if ($this->db->get($table, 1)->num_rows() > 0) {
        foreach ($this->$condition($data) as $column) {
            $this->db->where($column, $data[$column]);
        }
        $this->db->empty_table('gap_up');
        $this->db->insert(gap_up, $data);
        $this->db->update($table, $data);

    } else {
        $this->db->insert($table, $data);
    }
}
echo json_encode(['Last_record' => $this->db->where('date', $data['date'])->get($table)->result_array()]);

问题分析与修正方案

问题点

  1. 清空表逻辑位置错误:将empty_table放在循环内的if分支中,仅当当前处理的$table存在数据时才会执行清空,且每处理一行数据就清空一次gap_up表,导致之前插入的行被覆盖;若当前$table不是gap_up,这部分逻辑根本不会触发。
  2. 表名未加引号:insert(gap_up, $data)中的gap_up未用单引号包裹,PHP会将其视为常量,若未定义则触发警告,导致插入操作失败。
  3. gap_up表逻辑混淆:该表需求是每次导入前清空再全量插入,不需要执行更新逻辑,应与其他表的更新逻辑分离。

修正后的代码

$records = $this->csvimport->parse_csv($this->directory . $file);

// 处理gap_up表:导入前先清空
if ($table === 'gap_up') {
    $this->db->empty_table('gap_up');
}

foreach ($records as $row) {
    $data = $this->$function($row);
    $data = $this->correct_date_format($data);

    // 针对gap_up表直接插入(已提前清空)
    if ($table === 'gap_up') {
        $this->db->insert('gap_up', $data);
        continue;
    }

    // 其他表执行原有更新/插入逻辑
    foreach ($this->$condition($data) as $column) {
        $this->db->where($column, $data[$column]);
    }

    if ($this->db->get($table, 1)->num_rows() > 0) {
        foreach ($this->$condition($data) as $column) {
            $this->db->where($column, $data[$column]);
        }
        $this->db->update($table, $data);
    } else {
        $this->db->insert($table, $data);
    }
}

echo json_encode(['Last_record' => $this->db->where('date', $data['date'])->get($table)->result_array()]);

修正说明

  • 将gap_up表的清空逻辑移到循环外,确保仅在导入前清空一次。
  • 对gap_up表单独处理,直接执行插入操作,跳过原有更新逻辑。
  • 修正插入时的表名,用单引号包裹'gap_up',避免常量解析错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 12:05:33