在Codeigniter中更新多行 [英] Updating Multiple Rows in Codeigniter
问题描述
我可以在数据库中插入多行,但问题是当我试图更新它给我一个Codeigniter错误消息。这是我的模型,实际上我没有什么重要的控制器中发现错误,是因为我只是加载控制器中的模型。
$ data = array();
// $ todayDate = date('Y-m-d');
for($ i = 1; $ 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;
解决方案 code>以下部分应为
$ data [] = array(
$ code ='code' => $ _POST ['code'] [$ i],
'price'=> $ _POST ['sell']
);
替换为
$ data [] = array(
'code'=> $ _POST ['code'] [$ i],
'price'=> $ _POST ['sell' ]
);
并更新您应该使用的值 update_batch
而不是 insert_batch
$ this-& update_batch('yourtableName',$ data,'code'); //'code'is where key
替换 yourtableName
与您的原始表名和代码
正在用于其中
键,因此您不需要使用 $ this-> db->其中('code',$ code)
。
参考: 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;
解决方案 In your Model
following part should be
$data[] = array(
$code='code' => $_POST['code'][$i],
'price' => $_POST['sell']
);
replaced with
$data[] = array(
'code' => $_POST['code'][$i],
'price' => $_POST['sell']
);
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
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)
.
Reference: CodeIgniter.
这篇关于在Codeigniter中更新多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!