在 Codeigniter 中更新多行

2022-01-17 00:00:00 sql-update sql php codeigniter

我可以在数据库中插入多行,但问题是当我尝试更新它时,它会给我一个 Codeigniter 错误消息.这是我的模型,实际上我在控制器中没有什么重要的发现错误,是因为我只是在控制器中加载了该模型.

I can insert multiple rows in the database, but the problem is when I am trying to update it gives me a Codeigniter error message. Here is my model, actually I have nothing significant in the controller to find an error in, is because I just load that model in controller.

$data  = array();
//$todayDate = date('Y-m-d');
for($i = 1; $i < count($_POST['code']); $i++) {
    //$code=$_POST['code'][$i];
    if($_POST['code'][$i] != '') {
        $data[] = array(
            $code='code' => $_POST['code'][$i],
            'price' => $_POST['sell']
            );
    }
}
$linksCount = count($data);

if($linksCount) {
    $this->db->where('code',$code);
    $this->db->insert_batch('sell_rate', $data);
}

return $linksCount;

推荐答案

在你的Model下面的部分应该是

In your Model following part should be

$data[] = array(
    $code='code' => $_POST['code'][$i],
    'price' => $_POST['sell']
);

替换为

$data[] = array(
    'code' => $_POST['code'][$i],
    'price' => $_POST['sell']
);

要更新值,您应该使用 update_batch 而不是 insert_batch

and to update the values you should use update_batch instead of insert_batch

$this->db->update_batch('yourtableName', $data, 'code'); // 'code' is where key

yourtableName 替换为您的原始表名,并且 code 用于 where 键,因此您不需要使用 $this->db->where('code',$code).

Replace yourtableName with your original table name and code is being used for where key, so you don't need to use $this->db->where('code',$code).

参考: CodeIgniter.

相关文章