Mohsin
Mohsin

Reputation: 485

Problem in updating multiple row with multiple condition in CodeIgniter

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);
}

Upvotes: 0

Views: 173

Answers (1)

Fazlan Ahamed
Fazlan Ahamed

Reputation: 241

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');

Upvotes: 1

Related Questions