qwerty
qwerty

Reputation: 5246

Codeigniter/Mysql: Column count doesn't match value count with insert_batch()?

Alright, so i have a huge list (like 500+) of entries in an array that i need to insert into a MySQL database.

I have a loop that populates an array, like this:

$sms_to_insert[] = array(
    'text' => $text,
    'contact_id' => $contact_id,
    'pending' => $status,
    'date' => $date,
    'user_id' => $this->userId,
    'sent' => "1"
);

And then i send it to the database using the built insert_batch() function:

public function add_sms_for_user($id, $sms) {
    //$this->db->delete('sms', array("user_id" => $id)); Irrelevant
    $this->db->insert_batch('sms', $sms); // <- This!
}

The error message i get is as follows: Column count doesn't match value count at row 1.

Now, that doesn't make sense at all. The columns are the same as the keys in the array, and the values are the keys value. So, why is it not working?

Any ideas?

Upvotes: 1

Views: 1351

Answers (1)

qwerty
qwerty

Reputation: 5246

user_id turned out to be null in some situations, that's what caused the error.

EDIT: If you replace insert_batch() with a loop that runs insert() on the array keys you will get more clear error messages.

Upvotes: 2

Related Questions