如何使用 CodeIgniter 在数据库中插入多个值?

Posted

技术标签:

【中文标题】如何使用 CodeIgniter 在数据库中插入多个值?【英文标题】:how to insert multiple values in database using CodeIgniter? 【发布时间】:2019-12-27 08:43:50 【问题描述】:

我尝试使用 codeignitor 在 mysql 数据库中插入多个数据值。但它不起作用

我收到以下错误 error db error

INSERT INTO `allocated` (0, 1) VALUES ('1463104690','9443610181'), ('Timing-Device Piston','Pump element '), ('Bosch Germany','Bosch Germany'), ('17.07.2019','17.07.2019'), ('17.07.2019','17.07.2019'), ('Nigeria','Nigeria'), ('AGL-0014','AGL-0014'), ('302-05861181-19','302-05861181-19'), ('142417201432','142417201432'), ('84139100','84139100'), ('INDIA','JAPAN'), ('20','24'), ('',''), ('20','24'), ('10','14'), ('20',''), ('21.52','14.7'), ('',''), ('',''), ('',''), ('215.2','205.8'), ('0.056','0.136'), ('0.56','1.904'), ('',''), ('',''), ('',''), (), ('3785174','351715735'), ('xyz','abc')

文件名:D:/wamp/www/aal-stock/system/database/DB_driver.php

行号:691

控制器:

function itemsallocated()
    
        if($this->session->userdata('Username') != '')
        
            $this->load->model("main_model");
            $pn=$this->input->post('Part_Number');
            $desc=$this->input->post('Description');
            $brand=$this->input->post('From');
            //$pfrom=$this->input->post('From');
            $idate=$this->input->post('Date');
            $impcode=$this->input->post('Import_License_Number');

            $invused=$this->input->post('Invoice_Used');
            $invno=$this->input->post('Invoice_Number');
            $decno=$this->input->post('Declaration_Number');
            $billno=$this->input->post('Bill_Number');

            $hscode=$this->input->post('HS_Code');
            $coo=$this->input->post('COO');
            $qty=$this->input->post('Quantity');
            $Stock_Rem=$this->input->post('Stock_Rem');
            $Stock_Rem2=$this->input->post('Stock_Rem2');
            $cart=$this->input->post('cart');
            $Allocated=$this->input->post('Allocated');


            $euro=$this->input->post('Price_in_Euro');
            $usd=$this->input->post('Price_in_USD');
            $aed=$this->input->post('AED');
            $jpy=$this->input->post('JPY');

            $value=$this->input->post('Value');
            $wpp=$this->input->post('Wt_Per_Piece');
            $twt=$this->input->post('Total_Wt');

            $L=$this->input->post('L');
            $B=$this->input->post('B');
            $H=$this->input->post('H');

            $Added_By=$this->input->post('Added_By');
            $Cus_Name=$this->input->post('Cus_Name');

            date_default_timezone_set('Asia/Dubai');
            $ddate= date('d-m-Y H:i:s');


    $data=array('Part_Number'=>$pn,
                'Description'=>$this->input->post('Description'),
                'From'=>$brand,
                'Date'=>$idate,

                'Import_License_Number'=>$impcode,
                'Invoice_Used'=>$invused,
                'Invoice_Number'=>$invno,
                'Declaration_Number'=>$decno,

                'Bill_Number'=>$billno,
                'HS_Code'=>$hscode,
                'COO'=>$coo,
                'Quantity'=>$qty,

                'Stock_Rem'=>$Stock_Rem,
                'Stock_Rem2'=>$Stock_Rem2,
                'cart'=>$cart,
                'Allocated'=>$Allocated,


                'Price_in_Euro'=>$euro,
                'Price_in_USD'=>$usd,
                'AED'=>$aed,
                'JPY'=>$jpy,
                'Value'=>$value,
                'Wt_Per_Piece'=>$wpp,
                'Total_Wt'=>$twt,

                'L'=>$L,
                'B'=>$B,
                'H'=>$H,

                'd_t'=>$ddate,
                'Added_By'=>$Added_By,
                'Cus_Name'=>$Cus_Name


                );

$this->db->insert_batch('allocated', $data);

【问题讨论】:

请分享完整和准确的错误信息 【参考方案1】:

在 CodeIgniter 中插入多个数据值:

$data = array(
   array(
      'title' => 'My title' ,
      'name' => 'My Name' ,
      'date' => 'My date'
   ),
   array(`enter code here`
      'title' => 'Another title' ,
      'name' => 'Another Name' ,
      'date' => 'Another date'
   )
);

$this->db->insert_batch('table_name', $data); 

【讨论】:

【参考方案2】:

你也可以这样使用:

foreach($_POST['data'] as $data)

  $this->db->insert('table_name', $data);

【讨论】:

请在您的答案中添加一些解释,以便其他人可以从中学习 - 您应该强调如果插入大量数据,此解决方案存在严重缺陷

以上是关于如何使用 CodeIgniter 在数据库中插入多个值?的主要内容,如果未能解决你的问题,请参考以下文章

如何在codeigniter中使用ajax在mysql数据库中插入数据?

如何使用codeigniter在数据库中插入图像

如何在 Codeigniter 中插入动态数据?

如何在 CodeIgniter 中获取活动记录插入的结果

如何在 CodeIgniter 4 中保存到数据库后获取插入 ID

如何在 Codeigniter 中的数据库中插入数组?