Insert multiple rows using for loop Insert multiple rows using for loop codeigniter codeigniter

Insert multiple rows using for loop


Try it:

$sql = "INSERT INTO pl_tbl (p_id,po_name,po_val) VALUES";$values = [];for ($i=0;$i<$count;$i++){    $values[] = "('$id','$data['data']['name_$i]','$data['data']['val_$i]')";}$sql .= join(',', $values);$result = mysql_query($sql);


Codeigniter active record has a function insert_batch i think that is what you need:

$data = array(array(  'p_id' => 'My id' ,  'po_name' => 'My Name' ,  'po_val' => 'My val'),array(  'title' => 'Another title' ,  'name' => 'Another Name' ,  'date' => 'Another date'));$this->db->insert_batch('pl_tbl', $data); 

Other way:

for ($i=0;$i<$count;$i++)   {     $data = array(      array(      'p_id' => $id ,      'po_name' => $data['data']['name_'.$i] ,      'po_val' => $data['data']['val_'.$i]     );     $this->db->insert('pl_tbl', $data);   }


try this simple code its should work for you..

<?php  for($i=0;$i<$count;$i++)  {     $sql="INSERT INTO pl_tbl (p_id,po_name,po_val) VALUES ('$id','$data['data']['name_'.$i]','$data['data']['val_'.$i]')";     $result = mysql_query($sql);  }?>

thanks