Post system - "Duplicate entry '0' for key 'PRIMARY"

4

I'm having a problem, I'm not really sure if it's in PHP or in the database.

In a posting system using PHP, as soon as you submit a notice, ID (the primary key, which would be used like this: index.php?ID=0 ) is not changing, ie it always starts in 0 and I wanted it to start from 1 , and when I go to create a post it ends up giving the following error:

  

Duplicate entry '0' for key 'PRIMARY' "

and there I always have to change in MySQL to another ID .

Code: link

Would you like to put sequentially?

    
asked by anonymous 20.07.2015 / 15:07

2 answers

8

Your problem is probably in the configuration of the table. ID field must be primary key and auto increment . As it was done (without auto increment ) php informs null to mysql that saves primary key to zero every time you run your code.

    
20.07.2015 / 15:09
4

In the database you must use auto_increment in the main ID. So when adding a new record, it already adds a new id for this record.

ALTER TABLE 'suatabela' CHANGE 'id' 'id' INT(10) NOT NULL AUTO_INCREMENT;
    
20.07.2015 / 15:12