繁体   English   中英

Insert_batch CodeIgniter

[英]Insert_batch CodeIgniter

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

这是控制器中的代码:

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

未知列应该是数量、价格和产品 ID

我做错了什么?

在控制器中

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

在模型中

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

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

// Produces: 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); }

// Produces: INSERT INTO orders (quantity, price, productID) VALUES ('$quantity', '$price', '$product_id'),('$quantity2', '$price2', '$product_id2')

您尝试使用函数insert_batch

所以检查文档:

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

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

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

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

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

暂无
暂无

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

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