manju manju - 4 years ago 212
PHP Question

how to insert multiple array to database using codeigniter

I have posted values $a,$b,$c...are in array format
model file i gave this.

function insert_bank_data($da,$b,$c,$d,$e)
{

$data=array('date'=>$da,'des'=>$b,'amount'=>$c,'price'=>$d);
$this->db->insert_batch('total_trans',$data);


}


I am getting an error like this.

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22' at line 1

Answer Source

if arguments of the function are arrays of the same length you should write

function insert_bank_data($da,$b,$c,$d,$e)
{
  $data = array();
  for ($i-0; $i < count($da); $i++) {
    $data[] = array('date'=>$da[$i],'des'=>$b[$i],'amount'=>$c[$i],'price'=>$d[$i]);
  $this->db->insert_batch('total_trans',$data);
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download