Reputation: 65
I have a problem with Codeigniter's insert_batch model function, it seems to rearrange the array that is passed on it in ascending. here's my code
Controller (gets all the post data and arrange it into an array):
$product_id = $this->input->post('product_id');
$colors = $this->input->post('color_name');
$quantity = $this->input->post('product_quantity');
$payment_option = $this->input->post('payment_option');
$price = $this->input->post('product_price');
$date = date('Y-m-d');
$orders = array();
$other_info = $this->business_mgmt->unique_details();
for($i = 0; $i < count($product_id); $i++) {
$color_id = $this->products_model->select_color_id($colors[$i]);
$orders[] = array(
'order_id' => null,
'invoice_number' => $other_info[0],
'customer_number' => $other_info[1],
'user_id' => 1,
'product_id' => $product_id[$i],
'color_id' => $color_id,
'quantity' => $quantity[$i],
'price' => $price[$i],
'order_date' => $date
);
}
$this->business_mgmt->insert_order($orders);
and here's the model:
function insert_order($order_details) {
$this->db->insert_batch('exp_mcc_orders', $order_details);
print '<pre>';
print_r($order_details);
print '</pre>';
}
and here's the error message:
Error Number: 1062
Duplicate entry '0' for key 'PRIMARY'
INSERT INTO `exp_mcc_orders`
(`color_id`, `customer_number`, `invoice_number`,`order_date`, `order_id`, `price`,
`product_id`, `quantity`, `user_id`)
VALUES ('2','260','20130876617','2013-08-27',NULL,'15','4','4',1)
Filename: C:\xampp\htdocs\MiracleCandleCompany\website\system\database\DB_driver.php
Line Number: 330
Advance thanks. The right order of the columns in my database is the one I used on my array. The problem is codeigniter rearranges it in ascending.
Upvotes: 2
Views: 3703
Reputation: 65
Just got it working, my order_id(primary key) isn't set to auto_increment and I mistakenly set the other columns into UNIQUE KEY.
Upvotes: 0
Reputation: 396
Try to remove key order_id
from array $orders
. Also You can check settings of table exp_mcc_orders
for column order_id
- set auto increment.
Upvotes: 1