BetaArchive Logo
Navigation Home Screenshots Image Uploader Server Info FTP Servers Wiki Forum RSS Feed Rules Please Donate
UP: 30d, 2h, 5m | CPU: 2% | MEM: 5902MB of 11046MB used
{The community for beta collectors}

Forum rules


Any off topic discussions should go in this forum. Post count is not increased by posting here.
FTP Access status is required to post in this forum. Find out how to get it


Post new topic Reply to topic  [ 8 posts ] 
Author Message
 PostPost subject: Duplicate entry '0' for key 'PRIMARY' [1062]        Posted: Wed Feb 04, 2009 5:16 pm 
Reply with quote
Staff
User avatar
Offline

Joined
Thu Nov 30, 2006 6:10 pm

Posts
2647

Location
Land of The Tea.

Favourite OS
Windows 10 Pro x64
Get that error when posting:
Image

_________________
Image
| Personal site - Social links - PC setup | TF2 Game Servers |
Liking traps isn't gay, its not gay if it looks like a girl
I-i-it's n-not as if I wanted to ban you or anything. B-baka. (「・ω・)「


Top  Profile  WWW
 PostPost subject: Re: Duplicate entry '0' for key 'PRIMARY' [1062]        Posted: Wed Feb 04, 2009 7:46 pm 
Reply with quote
Donator
Offline

Joined
Tue Oct 17, 2006 8:26 pm

Posts
932
This says exactly what it means. You are trying to insert a record with a primary key of 0, and there is already a record with a primary key value of 0. Primary keys must be unique. I'd imagine you aren't specifying a value for that field and you have set the default of that field to 0.

I don't know what database system you are using, but you probably should make your primary key of that table an autonumber/autoincrement or modify your insert statement to retrieve one (Something like [Insert into MyTable (MyField1, ...) Values (GetUniqueID(), ...)])


Top  Profile
 PostPost subject: Re: Duplicate entry '0' for key 'PRIMARY' [1062]        Posted: Thu Feb 05, 2009 9:49 pm 
Reply with quote
Staff
User avatar
Offline

Joined
Thu Nov 30, 2006 6:10 pm

Posts
2647

Location
Land of The Tea.

Favourite OS
Windows 10 Pro x64
Could you simplify? I don't follow

_________________
Image
| Personal site - Social links - PC setup | TF2 Game Servers |
Liking traps isn't gay, its not gay if it looks like a girl
I-i-it's n-not as if I wanted to ban you or anything. B-baka. (「・ω・)「


Top  Profile  WWW
 PostPost subject: Re: Duplicate entry '0' for key 'PRIMARY' [1062]        Posted: Fri Feb 06, 2009 1:37 am 
Reply with quote
Donator
Offline

Joined
Tue Oct 17, 2006 8:26 pm

Posts
932
Ok, let's start simple. What is the primary key field of that table?


Top  Profile
 PostPost subject: Re: Duplicate entry '0' for key 'PRIMARY' [1062]        Posted: Sat Feb 07, 2009 12:13 am 
Reply with quote
Staff
User avatar
Offline

Joined
Thu Nov 30, 2006 6:10 pm

Posts
2647

Location
Land of The Tea.

Favourite OS
Windows 10 Pro x64
Emmm, it's "post_id"

_________________
Image
| Personal site - Social links - PC setup | TF2 Game Servers |
Liking traps isn't gay, its not gay if it looks like a girl
I-i-it's n-not as if I wanted to ban you or anything. B-baka. (「・ω・)「


Top  Profile  WWW
 PostPost subject: Re: Duplicate entry '0' for key 'PRIMARY' [1062]        Posted: Sun Feb 08, 2009 2:30 am 
Reply with quote
Donator
Offline

Joined
Tue Oct 17, 2006 8:26 pm

Posts
932
Ok, so you'll notice that post_id isn't specified anywhere in your INSERT statement. That means that it is defaulting to 0 as the value since you aren't specifying it.


Top  Profile
 PostPost subject: Re: Duplicate entry '0' for key 'PRIMARY' [1062]        Posted: Wed Feb 11, 2009 6:48 pm 
Reply with quote
Staff
User avatar
Offline

Joined
Thu Nov 30, 2006 6:10 pm

Posts
2647

Location
Land of The Tea.

Favourite OS
Windows 10 Pro x64
I still don't under stand since me being n00b at MYSQL :P

_________________
Image
| Personal site - Social links - PC setup | TF2 Game Servers |
Liking traps isn't gay, its not gay if it looks like a girl
I-i-it's n-not as if I wanted to ban you or anything. B-baka. (「・ω・)「


Top  Profile  WWW
 PostPost subject: Re: Duplicate entry '0' for key 'PRIMARY' [1062]        Posted: Sat Feb 14, 2009 7:48 pm 
Reply with quote
Donator
Offline

Joined
Tue Oct 17, 2006 8:26 pm

Posts
932
Ok, lets use an example. I have a table defined as such:
Code:
Create Table MyTable
(
    MyPrimaryKey Int Not Null Primary Key Default 0,
    MyField1 VarChar(100) Not Null Default "",
    MyField2 Date
)


Now, MyPrimaryKey is defined as a Primary Key. This means that is HAS to be unique in the table. Also we defined it to not allow a NULL value. The "Default 0" part means that if you don't specify a value when inserting a record, 0 will be the default value. This is similar to how I defined MyField1. It can't be NULL and if you don't specify a value, it defaults to a value of "".

So take the following insert statement:

Code:
Insert Into MyTable (MyPrimaryKey, MyField1, MyField2) Values (100, "Hello", "1/1/2009");


This would insert a record with MyPrimaryKey=100, MyField1="Hello", and MyField2="1/1/2009"

Now for another one:

Code:
Insert Into MyTable (MyPrimaryKey, MyField2) Values (101, "1/1/2009");


Notice that I didn't specify the MyField1 record? That means I will end up with a record that has MyPrimaryKey=101, MyField1="", and MyField2="1/1/2009", because it used the specified default value for MyField1.

Now lets say I run that SAME statement again:

Code:
Insert Into MyTable (MyPrimaryKey, MyField2) Values (101, "1/1/2009");


This will now fail. The reason? There is already a record in the table where MyPrimaryKey=101. You would get the very error message that you are getting "Duplicate Entry '101' for key 'PRIMARY'".

Now, to be more direct to your problem, lets change our example table a bit... notice that if I don't specify a value for MyPrimaryKey, it defaults to 0? That is probably exactly how you have it defined. And since you aren't specifying a value for that field when you insert a new record, it defaults to 0. But since it is a primary key, there can ONLY be one record where MyPrimaryKey=0, so after the first record gets inserted, it will fail with the message "Duplicate Entry '0' for key 'PRIMARY'"

I hope that helps because I'm not sure I can be any more clear.


Top  Profile
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 




Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Jump to:  

All views expressed in these forums are those of the author and do not necessarily represent the views of the BetaArchive site owner.

Powered by phpBB® Forum Software © phpBB Group

Copyright © 2006-2018

 

Sitemap | XML | RSS