-3

I am currently starting web development and am now working on a very simple script that posts entry data from a from to a mysql database. But the problem I have been encountering is that when I submit the form I get the following error:

Error: Duplicate entry '0' for key 'PRIMARY'

To me it seems a really weird error as the table is completely empty, ID is set to auto_increment and I am not trying to assign any value to it.

I am using Xampp for my localhost on Mac OS btw.

This is my form (.php):

This is my mysql entry script (.php):

The result is this:

This is the database setup:

The weird thing is that when the table is empty, and I insert through the form the first time, something does end up in the database. But it is missing "email" and "password" and shows "NULL". The second time I use the form, nothing is added in the database:

deceze
  • 510,633
  • 85
  • 743
  • 889
Mark de Jonge
  • 73
  • 1
  • 11
  • 2
    Please put any codes as text or links to texts, nobody has time to re-type it in order to debug it for you. Also read http://stackoverflow.com/tour and this may be of help http://stackoverflow.com/help/formatting – DeDee Feb 19 '15 at 01:21
  • 1
    you doing 3 inserts first one will work and add key 0 2nd will fail due to the duplicate - you shouldn't be doing 3 inserts regardless, just 1 –  Feb 19 '15 at 01:25

1 Answers1

1
  1. Your id column does not seem to be auto-incrementing. It is not generating a new id for every row. It just defaults to 0 if you don't supply a value for it.
  2. Nonetheless there's a UNIQUE/PRIMARY constraint on that column, so the id 0 cannot occur more than once. Since you're not supplying an id and MySQL isn't generating one (because the column isn't auto-incrementing), you cannot insert more than one row.
  3. You're issuing three separate INSERT INTO statements which will result in three separate rows to be inserted (if you could insert more than one row, see above), each with a different value set; but never one row with all values set.

So:

  1. Make your id column actually auto-incrementing.
  2. Prepare a single INSERT INTO statement which inserts all values in one go:

    INSERT INTO members (name, email, password) VALUES (.., .., ..)
    
  3. See Why shouldn't I use mysql_* functions in PHP? and stop using mysql_*. Learn about prepared statements, read The Great Escapism (Or: What You Need To Know To Work With Text Within Text) for why.

Community
  • 1
  • 1
deceze
  • 510,633
  • 85
  • 743
  • 889