I am trying to insert some values coming from a form as array using the function insert_batch with CodeIgniter.
This the code in the controller:
$data_product = array(
'quantity'=> $quantity,
'price'=> $price,
'productID'=> $product_id
);
$this->load->model('product_ordered');
$this->product_ordered->insert_quantity_products($data_product);
This is the code in the model insert_quantity_products
public function insert_quantity_products($data){
$this->db->insert_batch('orders', $data);
}
And this is the database error
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')
The unknown columns should be quantity, price and productID
What I am doing wrong?
in controller
$this->load->model('product_ordered');
$this->product_ordered->insert_quantity_products();
in model
if you are using single input use 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')
if you are yousing batch insert use this ($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')
Your try to use function insert_batch
.
So check documentation:
The first parameter will contain the table name, the second is an associative array of values.
$data = array(
array(
'title' => 'My title' ,
'name' => 'My Name' ,
'date' => 'My date'
),
array(
'title' => 'Another title' ,
'name' => 'Another Name' ,
'date' => 'Another date'
)
);
This is example how you must set your data.