2

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.

2
  • 1
    I don't know the framework you're using, but the order of columns does not matter. The problem is your primary key doesn't auto-increment. Commented Aug 26, 2013 at 21:33
  • 1
    Please make your question titles more descriptive in the future. “PHP CodeIgniter” doesn’t really help people in searches. Commented Aug 26, 2013 at 21:48

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.