简体   繁体   中英

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.

在此处输入图片说明

And a database that looks like:

id: 3 val: 37.10119357072203

-

id: 4 val: -122.06634521484374

I want to insert the array value into the 'val' field based on the array key matching the database 'id' field. How do I do this using codeigniter's 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');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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