Alex Naspo
Alex Naspo

Reputation: 2102

how to use insert_batch with unique ids

I am using codeignitor's insert batch function to insert multiple rows to a table.

    $this->db->insert_batch('table', $sizes);

my $sizes array looks like this

 $sizes = array(
   array(
      'size' => 'M' ,
      'product' => 'Hat' 
   ),
   array(
      'size' => 'L' ,
      'product' => 'Hat' 
   )
);

I was intending for separate rows to be added to my db table with these values along with unique ids, however when each nested array is added, It adds 0 to the id field, rather than a unique ID

I am receiving the error "Duplicate entry '0' for key 'id'"

What is the best approach to solve this? Thanks for reading!

Upvotes: 0

Views: 322

Answers (1)

Ts8060
Ts8060

Reputation: 1070

Make your UNIQUE Column with AUTO_INCREMENT

Upvotes: 2

Related Questions