Update multiple Rows in Codeigniter Update multiple Rows in Codeigniter codeigniter codeigniter

Update multiple Rows in Codeigniter


Are you using Active record?

Yes there is an update batch: $this->db->update_batch();

$data = array(array(  'ID' => 1 ,  'Settings Name' => 'Hello' ,  'Settings Value' => 'True'),array(  'ID' => '2' ,  'Settings Name' => 'World' ,  'Settings Value' => 'Good'));    $this->db->update_batch('mytable', $data, 'where_key'); 

From the documentation:

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


There is indeed an update_batch() method available in CodeIgniter already.

You can use it your example like so:

$data = array(    array(        'ID' => 1,        'Settings Name' => 'Hello',        'Settings Value' => 'True'    ),    array(        'ID' => 2,        'Settings Name' => 'World',        'Settings Value' => 'Good'    ));$this->db->update_batch('tableName', $data, 'id'); 

So what you have is an array of arrays, the children basically hold the data for each row in the database. The first parameter for update_batch() is the name of the database table, the second is the $data variable and the third is the column you want to use in the WHEN clause.


Here is a simple php code for perform this operations.

<?phpfunction basic_update($uwi='') {$this->load->database();$data = array('ID' => 1,'Settings Name' => 'Hello','Settings Value' => 'True'  );$this->db->where('ID', '1');$this->db->update('<table name>', $data);$data1 = array(    'ID' => 2,    'Settings Name' => 'World',    'Settings Value' => 'Good'    );    $this->db->where('ID', '2');    $this->db->update('<table name>', $data1);}

In $this->db->where('ID', '1'); ID is your table field and 1 is the value.In array first parameter will contain the table name, the second parameter is an associative array of values