I'm trying to insert a huge list of users to a MySQL database but everytime I try I get the error:
#1062 - Duplicate entry '' for key 2
It gives me this because the 2nd column is blank on quite a lot of the entries, so after it's inserted one blank entry in column 2, it won't let me add another. However, when I added most of the list yesterday I didn't get this error once even though a lot of the entries I added yesterday have a blank cell in column 2 as well. Whats going on?
This is the sql code to insert 1 entry. The rest follow the same format:
INSERT INTO users (`id`,`title`,`firstname`,`lastname`,`company`,`address`,`city`,`county` ,`postcode`,`phone`,`mobile`,`category`,`email`,`password`,`userlevel`) VALUES ('','','John','Doe','company','Streeet','city','county' ,'postcode','phone','','category','[email protected]','','');
The SHOW COUNT(*) ERRORS statement displays the number of errors. You can also retrieve this number from the error_count variable: SHOW COUNT(*) ERRORS; SELECT @@error_count; SHOW ERRORS and error_count apply only to errors, not warnings or notes.
The ERROR 1064 (42000) mainly occurs when the syntax isn't set correctly i.e. error in applying the backtick symbol or while creating a database without them can also create an error, if you will use hyphen in the name, for example, Demo-Table will result in ERROR 1064 (42000). Now database is created successfully.
In addition to Sabeen's answer:
The first column id is your primary key.
Don't insert ''
into the primary key, but insert null instead.
INSERT INTO users (`id`,`title`,`firstname`,`lastname`,`company`,`address`,`city`,`county` ,`postcode`,`phone`,`mobile`,`category`,`email`,`password`,`userlevel`) VALUES (null,'','John','Doe','company','Streeet','city','county' ,'postcode','phone','','category','[email protected]','','');
If it's an autoincrement key this will fix your problem.
If not make id
an autoincrement key, and always insert null
into it to trigger an autoincrement.
MySQL has a setting to autoincrement keys only on null
insert or on both inserts of 0
and null
. Don't count on this setting, because your code may break if you change server.
If you insert null
your code will always work.
See: http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html
Seems like the second column is set as a unique index. If you dont need that remove it and your errors will go away. Possibly you added the index by mistake and thats why you are seeing the errors today and werent seeing them yesterday
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With