简体   繁体   English

Codeigniter活动记录批量更新,其中where子句等于数组索引

[英]codeigniter active record batch update where where clause equals array index

I have an array of post data ($data) in codeigniter that looks like the attached image. 我在codeigniter中有一组帖子数据($ data),看起来像附加的图像。

在此处输入图片说明

And a database that looks like: 和一个数据库,看起来像:

id: 3 val: 37.10119357072203 id:3值:37.10119357072203

- --

id: 4 val: -122.06634521484374 id:4值:-122.06634521484374

I want to insert the array value into the 'val' field based on the array key matching the database 'id' field. 我想根据与数据库“ id”字段匹配的数组键将数组值插入“ val”字段。 How do I do this using codeigniter's update_batch. 如何使用codeigniter的update_batch做到这一点。 My model is currently: 我的模型目前是:

public function edit_config($data){
        $this->db->update_batch('extra_config', $data,'val');
    }

but I get the error: 但是我得到了错误:

One or more rows submitted for batch updating is missing the specified index.

在此处输入图片说明

You have to prepare your data its not normal not to touch it from the incoming request.. 您必须准备不正常的数据,以免从传入请求中获取数据。

public function edit_config($data){
     $updateData = array();
     foreach($data['val'] as $key=>$value) {
         $updateData[] = array('id'=>$key, 'val'=>$value);
     }

     $this->db->update_batch('extra_config', $updateData,'id');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM