Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Jun 2011
    Posts
    66
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Disallowing members to use same username as others

    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:

    Code:
    $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.

  • #2
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,556
    Thanks
    75
    Thanked 105 Times in 104 Posts
    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.
    Been a sign maker for 7 years. My business:
    American Made Signs

  • #3
    New Coder
    Join Date
    Jun 2011
    Posts
    66
    Thanks
    17
    Thanked 0 Times in 0 Posts
    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 :/

  • #4
    New Coder
    Join Date
    Jan 2012
    Posts
    10
    Thanks
    0
    Thanked 1 Time in 1 Post
    Quote Originally Posted by yazan View Post
    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?

  • #5
    New Coder
    Join Date
    Jun 2011
    Posts
    66
    Thanks
    17
    Thanked 0 Times in 0 Posts
    I try to register a member with the username "account".

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

  • #6
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Your code must be different than what you'd posted, it is adding single quotes around the back ticks for the username field name.

  • #7
    New Coder
    Join Date
    Jan 2012
    Posts
    10
    Thanks
    0
    Thanked 1 Time in 1 Post
    Quote Originally Posted by yazan View Post
    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.
    Last edited by Kekke; 01-13-2012 at 05:18 PM.

  • #8
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,474
    Thanks
    13
    Thanked 361 Times in 357 Posts
    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.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •