CodeIgniter Forums
Active Record, Auto Increment problem - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Active Record, Auto Increment problem (/showthread.php?tid=25629)



Active Record, Auto Increment problem - El Forum - 12-18-2009

[eluser]Unknown[/eluser]
Hello!

I am trying to insert a row into my database, the first time I tried it worked but the following times it did not. It does not work because one of the fields is an auto increment and CI gives it a value of 0 as default. I have looked in the documentation but I can not find anything.

This is my code:
Code:
$new_account_insert_data = array(
            'username'    => $username,
            'password'    => $password,
            'email'       => $email
        );
        
        $insert =  $this->db->insert('users', $new_account_insert_data);

My tables has theses fields: id (the auto increment one which I do not send in the query), username, password, email

This is the error message I get:


Quote:A Database Error Occurred

Error Number: 1062

Duplicate entry '0' for key 'PRIMARY'

INSERT INTO `users` (`username`, `password`, `email`) VALUES ('a-username', 'an-md5-password', '[email protected]')

Thank you.


Active Record, Auto Increment problem - El Forum - 12-18-2009

[eluser]Dirk Einecke[/eluser]
Hi,

add
Code:
'id'    => ''
to your data array.

Dirk


Active Record, Auto Increment problem - El Forum - 12-18-2009

[eluser]Unknown[/eluser]
Thank you very much Dirk. Such a simple fix.