Update multiple lines in Codeigniter

I can insert several rows into the database, but the problem is that when I try to update it, it gives me a Codeigniter error message. Here is my model, in fact I have nothing significant in the controller to find the error, because I just load this model into the controller.

$data = array(); //$todayDate = date('Ym-d'); for($i = 1; $i < count($_POST['code']); $i++) { //$code=$_POST['code'][$i]; if($_POST['code'][$i] != '') { $data[] = array( $code='code' => $_POST['code'][$i], 'price' => $_POST['sell'] ); } } $linksCount = count($data); if($linksCount) { $this->db->where('code',$code); $this->db->insert_batch('sell_rate', $data); } return $linksCount; 
+4
source share
1 answer

In your Model next part should be

 $data[] = array( $code='code' => $_POST['code'][$i], 'price' => $_POST['sell'] ); 

replaced by

 $data[] = array( 'code' => $_POST['code'][$i], 'price' => $_POST['sell'] ); 

and update the values ​​you should use update_batch instead of insert_batch

 $this->db->update_batch('yourtableName', $data, 'code'); // 'code' is where key 

Replace yourtableName your original table name and code used for the where key, so you do not need to use $this->db->where('code',$code) .

Link: CodeIgniter .

+3
source

All Articles