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

标签 php mysql codeigniter

将帖子值保存到数组。

$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
(
    [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
        )

)

将数组值插入数据库表。

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

我收到以下错误,错误是:

值作为数组返回。

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

最佳答案

获取帖子值:

$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');

遍历键值:

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

关于php - CodeIgniter 将多维数组作为单独的行插入到数据库表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14704472/

相关文章:

mysql - 同一个表上的多个 LEFT JOINS 是否有效?

mysql - NodeJs : how to resolve Error: connect ETIMEDOUT xx. xx.xx:xxxx

php - 用户登录无法正常工作未知错误

php - PHP 中的 json_encode 没有返回任何结果

php - 如何知道SQL查询是否不返回任何行

php - 将 `INT` 主键打包成单个 `LONG`

mysql - 在查询中将java时间转换为sqldate

javascript - 无法在codeIgniter PHP中接收ajax请求

php - CodeIgniter - 将 CSV 上传到数据库问题

javascript - 我在发布 JavaScript CodeIgniter 后没有得到响应