While working on a script using php and mysql, I encountered an error saying:
Duplicate entry ‘0’ for key 1
Duplicate entry ‘0’ for key 1 – php/mysql error
This error can be caused due to various issues depending upon the situation and your code / mysql database structure and where is the duplicate entry being caused.
However in my case, it was because the “id” field, despite being assigned unique and primary, wasn’t “autoincrement”.
How to solve Duplicate entry ‘0’ for key 1 – php/mysql error
So the solution is simply, edit the mysql table and make the field auto increment (your primacy key field usually).
To edit mysql table using phpmyadmin, goto phpmyadmin, select database and then the table. Click “structure” from top menu.
Click edit / change / pencil sign in front of the field (id in my case) which was giving duplicate entry error and make sure that auto_increment is ticked and selected.
Save the field and test your php code again, it shouldn’t give the error now inshaAllah.