douhan8892 2013-02-05 09:49
浏览 48
已采纳

CodeIgniter将多维数组作为单独的行插入到数据库表中

saving post value to an array.

$data2 = array(
  'invoice_id' =>($id),
  'order_id' => $this->input->post('order_id'),
  'invoiceitems_servicetype' =>$this->input->post('type'),
  'invoiceitems_quantity' => $this->input->post('quantity'),
  'invoiceitems_unitprice' => $this->input->post('rate'),
  'invoiceitems_notes' => $this->input->post('description')
      );

 $this->db->insert('abc_invoiceitems', $data2);

print_r($data2);

Array Structure.

Array
(
    [invoice_id] => 21
    [order_id] => 1
    [invoiceitems_servicetype] => Array
        (
            [0] => Order
            [1] => Miscellaneous
            [2] => Order
        )

    [invoiceitems_quantity] => Array
        (
            [0] => 5
            [1] => 64
            [2] => 88
        )

    [invoiceitems_unitprice] => Array
        (
            [0] =>  5
            [1] =>  6
            [2] =>  8
        )

    [invoiceitems_notes] => Array
        (
            [0] => test Data1
            [1] => test Data2
            [2] => test Data3
        )

)

Insert array values to database table.

TABLE NAME --- abc_invoiceitems 
 +------------+----------+--------------------------+-----------------------+------------------------+--------------------+
 | invoice_id | order_id | invoiceitems_servicetype | invoiceitems_quantity | invoiceitems_unitprice | invoiceitems_notes |
 +------------+----------+--------------------------+-----------------------+------------------------+--------------------+

I got this following error and the error is :

the values are returning as array.

INSERT INTO abc_invoiceitems (invoice_id, order_id, invoiceitems_servicetype, invoiceitems_quantity, invoiceitems_unitprice, invoiceitems_notes) VALUES ('21', '1', Array, Array, Array, Array)

  • 写回答

2条回答 默认 最新

  • dougu1896 2013-02-05 10:23
    关注

    Fetch post values:

    $temp =count($this->input->post('type'));//counting number of row's
    $type= $this->input->post('type');
    $quantity = $this->input->post('quantity');
    $rate = $this->input->post('rate');
    $description = $this->input->post('description');
    

    Loop through Key values:

    for($i=0; $i<$temp;$i++){
      $data2 = array(
      'invoice_id' =>($id),
      'order_id' => $this->input->post('order_id'),
      'invoiceitems_servicetype' =>$type[$i],
      'invoiceitems_quantity' => $quantity[$i],
      'invoiceitems_unitprice' => $rate[$i],
      'invoiceitems_notes' => $description[$i]
          );
       $this->db->insert('abc_invoiceitems', $data2);
      }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵