简体   繁体   English

如何使用Codeigniter将多个数组插入数据库

[英]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. 我已将值$ a,$ b,$ c ...发布到我给它的数组格式模型文件中。

 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

You can use json_encode() 您可以使用json_encode()

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

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

}

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

You can use this also... 您也可以使用此...

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

    for ($i=0; $i < count($da); $i++) {
        $data= array('date'=>$da[$i],'des'=>$b[$i],'amount'=>$c[$i],'price'=>$d[$i]);
        $this->db->insert('total_trans',$data);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM