0

I am trying to make a multiple-row update in Codeigniter. I am getting an error:

A Database Error Occurred
One or more rows submitted for batch updating is missing the specified index.

I think the problem is within the model. I am putting the model function below please help me fix what I am doing wrong.

public function updateContacts($client_id){

    $data = array(
              array('address' => 'My address' ,
                    'name' => 'My Name 2' ,
                    ),
              array('address' => 'Another address' ,
                    'name' => 'Another Name 2' ,
                   )
     );
    $multipleWhere = ['tbl_contact.owner_id' => $client_id, 'tbl_contact.owner_type' => '3'];
    $this->db->update_batch('tbl_contact', $data, $multipleWhere);
}
1
  • 1
    can you show $data array/object please, but actually I don't think you can send an array as third parameter, check: codeigniter.com/userguide3/database/… Commented Aug 24, 2020 at 21:17

1 Answer 1

1

The third parameter should be the key as per the official document, not the where condition

The first parameter will contain the table name, the second is an associative array of values, the third parameter is the where key.

But try as below

   $data = array(
              array('address' => 'My address' ,
                    'name' => 'My Name 2' ,
                    ),
              array('address' => 'Another address' ,
                    'name' => 'Another Name 2' ,
                   )
     );

    $this->db->where('owner_id',$client_id);
    $this->db->update_batch('tbl_contact', $data, 'owner_type');
Sign up to request clarification or add additional context in comments.

3 Comments

Hi thank you for your answer. I have tried this $this->db->update_batch('tbl_contact', $data, 'key');. But looks like it updates all the row's columns with the value of the column I am trying to update. For example, there are 12 rows in total. I am fetching 6 rows where the key = 3. And I want to change one row from 6 and insert the rest of the 5 rows as they are. But those 5 rows also update with the same value of that one row I am trying to change. I hope I am able to make you understand. The situation is a bit complicated.

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.