Page 1 of 2

Duplicate entry '' for key 2

Posted: Fri Aug 20, 2010 9:10 am
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.

Re: Duplicate entry '' for key 2

Posted: Fri Aug 20, 2010 9:34 am
by NModern
try like this

Code: Select all

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

Re: Duplicate entry '' for key 2

Posted: Fri Aug 20, 2010 10:11 am
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

Re: Duplicate entry '' for key 2

Posted: Fri Aug 20, 2010 10:19 am
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

Re: Duplicate entry '' for key 2

Posted: Fri Aug 20, 2010 10:23 am
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.

Re: Duplicate entry '' for key 2

Posted: Fri Aug 20, 2010 10:47 am
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)");

Re: Duplicate entry '' for key 2

Posted: Fri Aug 20, 2010 10:52 am
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.

Re: Duplicate entry '' for key 2

Posted: Fri Aug 20, 2010 11:26 am
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?

Re: Duplicate entry '' for key 2

Posted: Fri Aug 20, 2010 12:53 pm
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.

Re: Duplicate entry '' for key 2

Posted: Fri Aug 20, 2010 3:08 pm
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

Re: Duplicate entry '' for key 2

Posted: Fri Aug 20, 2010 3:20 pm
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!

Re: Duplicate entry '' for key 2

Posted: Fri Aug 20, 2010 3:25 pm
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

Re: Duplicate entry '' for key 2

Posted: Fri Aug 20, 2010 3:36 pm
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.

Re: Duplicate entry '' for key 2

Posted: Fri Aug 20, 2010 3:38 pm
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

Re: Duplicate entry '' for key 2

Posted: Fri Aug 20, 2010 3:44 pm
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 :/