Duplicate entry '' for key 2

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

bian101
Forum Newbie
Posts: 19
Joined: Fri Aug 20, 2010 9:05 am

Duplicate entry '' for key 2

Post by bian101 »

Hey guys!

I know, I know this problem is EVERYWHERE but i just dont understand! I have a solid knowlage of php but my SQL skills are low, so i dont know too much about Keys and stuff. But my error is: Duplicate entry '' for key 2.

My code is:

Code: Select all

<?    $sql = "SELECT * FROM users WHERE uid=".$uid;
    $fbid = mysql_query($sql);	
	$num_rows = mysql_num_rows($fbid);
	
	if(mysql_num_rows($fbid) < 1) {
		echo "You are not logged in.    ";
		mysql_query("INSERT INTO users (uid) VALUES ($uid)") 
		or die(mysql_error())
	;  
		
		} else {
		mysql_query("UPDATE users SET logged = '1' WHERE uid=".$uid);
		echo "Your Logged in ";
		echo $me['name'];
		//mysql_query("UPDATE users SET full_name = $me");
		?> Continue to <a href="removed :)"> My Settings </a>. <?			
	}
And specifically the bit causing the problem is:

Code: Select all

		mysql_query("INSERT INTO users (uid) VALUES ($uid)") 
So its telling me tht the uid exists is it not? When in the database its not there, however i think the cause to my problem is in the database there is a "0" (zero) in the columb so maybe because of the zero's its saying something is there? I dont know :) :) I would appriciate any help :)

Thanks,

Kind Regards,
Ian.
NModern
Forum Newbie
Posts: 11
Joined: Fri Aug 20, 2010 2:31 am

Re: Duplicate entry '' for key 2

Post by NModern »

try like this

Code: Select all

mysql_query("INSERT INTO `users` (`uid`) VALUES ('".$uid."')") 
bian101
Forum Newbie
Posts: 19
Joined: Fri Aug 20, 2010 9:05 am

Re: Duplicate entry '' for key 2

Post by bian101 »

NModern wrote:try like this

Code: Select all

mysql_query("INSERT INTO `users` (`uid`) VALUES ('".$uid."')") 
Hey,

No luck still getting Duplicate entry '' for key 2

My code now is:

Code: Select all

	if(mysql_num_rows($fbid) < 1) {
		echo "You are not logged in.    ";
		mysql_query("INSERT INTO `users` (`uid`) VALUES ('".$uid."')")
		or die(mysql_error());  
Any other ideas :S :S
shawngoldw
Forum Contributor
Posts: 212
Joined: Mon Apr 05, 2010 3:38 pm

Re: Duplicate entry '' for key 2

Post by shawngoldw »

How are you getting uid? It sounds like uid is a unique primary key and you are trying to create a new row in the database with a uid of 2 when you already have one in there.


Shawn
bian101
Forum Newbie
Posts: 19
Joined: Fri Aug 20, 2010 9:05 am

Re: Duplicate entry '' for key 2

Post by bian101 »

shawngoldw wrote:How are you getting uid? It sounds like uid is a unique primary key and you are trying to create a new row in the database with a uid of 2 when you already have one in there.


Shawn
Hey,

Sorry i should have said before this gets someones facebook info and loggs them in (facebook connect) and uid is a uniquie id from a facebook account that lets me id them. So here is where i call on the uid:

if ($session) {
try {
$uid = $facebook->getUser();
$me = $facebook->api('/me');

For example my uid is (just made this up) 655238384 then it puts it into the database (which it isnt). When i manually insert the uid it runs the ELSE part of my if:

Code: Select all

		} else {
		mysql_query("UPDATE users SET logged = '1' WHERE uid=".$uid);
		//mysql_query("UPDATE users SET full_name = $me WHERE uid=".$uid);

		echo "Your Logged in ";
		echo $me['name'];
		?> Continue to <a href="removed"> My Settings </a>. <?			
	}
And I dont quite understand the primary key bit :/

Kind regards,
Ian.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Duplicate entry '' for key 2

Post by AbraCadaver »

Well, here you are selecting the uid from users, so you know that it exists in the users table:

Code: Select all

$sql = "SELECT * FROM users WHERE uid=".$uid;
Then here you are trying to insert that same uid back into the users table, but it already exists there because you just selected it from there. Why???

Code: Select all

mysql_query("INSERT INTO users (uid) VALUES ($uid)");
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
bian101
Forum Newbie
Posts: 19
Joined: Fri Aug 20, 2010 9:05 am

Re: Duplicate entry '' for key 2

Post by bian101 »

AbraCadaver wrote:Well, here you are selecting the uid from users, so you know that it exists in the users table:

Code: Select all

$sql = "SELECT * FROM users WHERE uid=".$uid;
Then here you are trying to insert that same uid back into the users table, but it already exists there because you just selected it from there. Why???

Code: Select all

mysql_query("INSERT INTO users (uid) VALUES ($uid)");
Hey thats the thing, it doesnt exist as i use a mysql_num_rows so im saying

if(there is no record of their uid) {

insert their uid

else

echo "you have an account" etc.
bian101
Forum Newbie
Posts: 19
Joined: Fri Aug 20, 2010 9:05 am

Re: Duplicate entry '' for key 2

Post by bian101 »

Hmm, when i delete all rows from the database then it adds the uid to it, but after that the error returnes. I want to say something logical, but i cant think of anything :/

Any more ideas?
bian101
Forum Newbie
Posts: 19
Joined: Fri Aug 20, 2010 9:05 am

Re: Duplicate entry '' for key 2

Post by bian101 »

Is it anything to do with a key? When i made uid in teh db i dont think i put it in any key.
shawngoldw
Forum Contributor
Posts: 212
Joined: Mon Apr 05, 2010 3:38 pm

Re: Duplicate entry '' for key 2

Post by shawngoldw »

So the error comes when you try to login with a uid which has already been logged into?

If you're trying to login with uid, say, 21542 and you get the error. Check if 21542 is in the database already. It seems

Code: Select all

$sql = "SELECT * FROM users WHERE uid=".$uid;
    $fbid = mysql_query($sql);  
        $num_rows = mysql_num_rows($fbid);
        
        if(mysql_num_rows($fbid) < 1) {
is not returning you the right value.


Shawn
bian101
Forum Newbie
Posts: 19
Joined: Fri Aug 20, 2010 9:05 am

Re: Duplicate entry '' for key 2

Post by bian101 »

Hey Shawn,

Its working if the id is correct, but its not working if they dont have an account. Its a trickey one to describe.

For example, i delete my account so there is no record of me at all, i login and it adds the values to my database,

HOWEVER almost ALL other people who have tested it who do NOT have an account get the dupe error which is why i am so puzzeled!
shawngoldw
Forum Contributor
Posts: 212
Joined: Mon Apr 05, 2010 3:38 pm

Re: Duplicate entry '' for key 2

Post by shawngoldw »

By if the "id is correct", what exactly do you mean. How do you define correct?

You're saying that it works for you but not other people???

Shawn
bian101
Forum Newbie
Posts: 19
Joined: Fri Aug 20, 2010 9:05 am

Re: Duplicate entry '' for key 2

Post by bian101 »

shawngoldw wrote:By if the "id is correct", what exactly do you mean. How do you define correct?

You're saying that it works for you but not other people???

Shawn
Hey Shawn,

by correct i mean the uid is in the database so the num_rows would return 1 as its there. And it works for me and one other person out of about 5 so far.
shawngoldw
Forum Contributor
Posts: 212
Joined: Mon Apr 05, 2010 3:38 pm

Re: Duplicate entry '' for key 2

Post by shawngoldw »

That sounds weird. Print out the uid before the sql statement and make sure nothing weird is going on for you or the other people

Shawn
bian101
Forum Newbie
Posts: 19
Joined: Fri Aug 20, 2010 9:05 am

Re: Duplicate entry '' for key 2

Post by bian101 »

shawngoldw wrote:That sounds weird. Print out the uid before the sql statement and make sure nothing weird is going on for you or the other people

Shawn
Hey, i did that further up the code I put

echo $uid;

this is so weird, how can it work for me and not them :/
Post Reply