Updating Multiple Rows in Codeigniter(在 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.
这篇关于在 Codeigniter 中更新多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Codeigniter 中更新多行
基础教程推荐
- 在PHP中根据W3C规范Unicode 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 如何替换eregi() 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
