0

I have an array of post data ($data) in codeigniter that looks like the attached image.

enter image description here

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.

enter image description here

8
  • Can you dump this $data to see what you are sending to CI Commented Nov 25, 2015 at 15:33
  • can you echo the query using $this->db->last_query() and post it here? Commented Nov 25, 2015 at 15:37
  • @Svetlio - Array ( [val] => Array ( [3] => 37.10119357072203 [4] => -122.06634521484374 [5] => 37.51571709945411 ... is print_r($data) Commented Nov 25, 2015 at 15:51
  • @newCodex - I'm not sure where to do this echo - I tried placing it after the call to the edit_config function in my controller and I just get a blank page Commented Nov 25, 2015 at 15:54
  • 1
    @mheavers go to Codeigniter documentation and see how your array must be structured for update_batch as it is not the right way at the moment ;) Commented Nov 25, 2015 at 15:58

1 Answer 1

1

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');
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.