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

Asked

Viewed 4,670 times

5

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 post a news, the ID (the key Primary, which would be used so: index.php?ID=0) is not changing, that is, it always starts in the 0 and I wanted it to start from the 1, and when I create a post ends up giving the following error:

Duplicate entry '0' for key 'PRIMARY'"

and then I always have to change in Mysql to another ID.

Code: http://pastebin.com/raw.php?i=0dQEW0n1

It would have to be put in a sequential way?

  • Glue the database too... The problem is there!

  • That means you’re trying to insert another record with ID=0. You got the ID as auto increment?

2 answers

10


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

  • 1

    It’s exact. It always happens when I forget the AUTO_INCREMENT

  • 3

    @Wallacemaxters is why I always create the tables with Workbench

6

In the database you must use the auto_increment in the main ID. In order to add a new record, it already adds a new id to this record.

ALTER TABLE `suatabela` CHANGE `id` `id` INT(10) NOT NULL AUTO_INCREMENT;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.