Insert_batch CodeIgniter

时间:2015-05-26 19:09:50

标签: php codeigniter

我正在尝试使用带有CodeIgniter的函数insert_batch将来自表单的某些值作为数组插入。

控制器中的代码:

$data_product = array(
'quantity'=> $quantity,
'price'=> $price,
'productID'=> $product_id
);

$this->load->model('product_ordered');
$this->product_ordered->insert_quantity_products($data_product);

这是模型insert_quantity_products

中的代码
public function insert_quantity_products($data){
$this->db->insert_batch('orders', $data);
}

这是数据库错误

Unknown column '0' in 'field list'
INSERT INTO `orders` (`0`, `1`, `2`) VALUES ('3','1','1'),
('358.00','458.00','324.00'), ('1','39','69')

未知列应为数量,价格和productID

我做错了什么?

3 个答案:

答案 0 :(得分:2)

控制器中的

$this->load->model('product_ordered');
$this->product_ordered->insert_quantity_products();

在模型中

  1. 如果您使用单一输入,请使用此($this->db->insert

    public function insert_quantity_products() { $data_product = array( 'quantity'=> $quantity, 'price'=> $price, 'productID'=> $product_id );
    $this->db->insert('orders', $data_product); }

  2. //生成:INSERT INTO orders (quantity, price, productID) VALUES ('$quantity', '$price', '$product_id')

    1. 如果您要批量插入,请使用此($this->db->insert_batch

      public function insert_quantity_products() { $data_product = array( array( 'quantity'=> $quantity, 'price'=> $price, 'productID'=> $product_id ); array( 'quantity'=> $quantity2, 'price'=> $price2, 'productID'=> $product_id2 ); ); $this->db->insert_batch('orders', $data_product); }

    2. //生成:INSERT INTO orders (quantity, price, productID) VALUES ('$quantity', '$price', '$product_id'),('$quantity2', '$price2', '$product_id2')

答案 1 :(得分:0)

您尝试使用函数insert_batch

请检查文档:

第一个参数将包含表名,第二个参数是值的关联数组。

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

这是您必须如何设置数据的示例。

答案 2 :(得分:0)

您需要为$ data_product使用2维数组。 这意味着您将控制器更改$ data_product如下:

{ []}

相关问题