...

View Full Version : Disallowing members to use same username as others



yazan
01-13-2012, 12:43 PM
Hey,

When people want to register to my site, I of course don't want them register with the same usernames that already exist.
Now this is how I do it:


$check_name = mysql_query("SELECT `username` FROM `users` WHERE `username` = '$requested_name'");
if (mysql_num_rows($check_name) != 0) {
// Error, username taken
}

I'm pretty sure that's how it's supposed to be done. Even though I'm doing it right, it still doesn't work. I can register new memebrs with same username over and over again...

How do I solve this?

Thank you.

myfayt
01-13-2012, 01:01 PM
Everything appears fine to me. So I can get the problem is with $requested_name. It may not contain a value. If your using a post, make sure you are setting it correctly with the proper name.

yazan
01-13-2012, 01:04 PM
Everything is right, and it's set correctly with the proper name. There are no spelling mistakes or anything like that. It looks perfect, but it just doesn't work.

You can register and such, but you the problem is, I can have the same username as another member, or even 10 other members. That's not good :/

Kekke
01-13-2012, 01:39 PM
Everything is right, and it's set correctly with the proper name. There are no spelling mistakes or anything like that. It looks perfect, but it just doesn't work.

You can register and such, but you the problem is, I can have the same username as another member, or even 10 other members. That's not good :/

Have you tried echoing the SQL statement to see what has actually been inserted?

yazan
01-13-2012, 02:08 PM
I try to register a member with the username "account".

This is what it echoes: SELECT ID FROM members WHERE '`Username`' = 'account'

kbluhm
01-13-2012, 02:18 PM
Your code must be different than what you'd posted, it is adding single quotes around the back ticks for the username field name.

Kekke
01-13-2012, 03:04 PM
I try to register a member with the username "account".

This is what it echoes: SELECT ID FROM members WHERE '`Username`' = 'account'

Yes, as you can see as from the above post, remove the ' from `Username` and it should work.

Edit*

The reason of why this happens is because you are comparing `Username` as a string towards account. And unless you enter `Username` you will never get the same username.
Remove the ' and it will work.

Dormilich
01-14-2012, 09:24 AM
it would be also a good measure to set the username field to PRIMARY KEY or UNIQUE in the DB. that will definitely disallow duplicate unsernames.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum