Jump to content


Photo

Username Check


  • Please log in to reply
5 replies to this topic

#1 master82

master82
  • Members
  • PipPipPip
  • Advanced Member
  • 182 posts

Posted 12 June 2006 - 07:23 PM

I've created a registration form for my site and started the process of validating the input (check for blanks, length, passwords match etc) but I've come across a problem when it comes to checking the database to see if the requested username is already in use.

I've already included the connection info, from what I remember I have to do an IF(... SELECT and LIMIT or something like that.

Could anyone show me how to do this?

#2 Buyocat

Buyocat
  • Members
  • PipPipPip
  • Advanced Member
  • 267 posts

Posted 12 June 2006 - 07:25 PM

One way to check for a username already existing would be:
SELECT user_id FROM user_table WHERE username = '$username'
Make sure to replace the values in your table with mine, anyway if a user_id is returned then you know the username is taken, else you're safe to continue.
Looking for some easy-to-use tools?  Try these, https://sourceforge....jects/utils-php -- I made them myself.  They're distinct tools which are easy to understand and use.  See some examples uses at http://www.anotherearlymorning.com

#3 master82

master82
  • Members
  • PipPipPip
  • Advanced Member
  • 182 posts

Posted 12 June 2006 - 07:30 PM

Thanx but I need a way of automatically checking if it is present, hence why I mentioned the IF before (If its already taken then i'll die("username already taken") but if no match is found then the script will continue to add the new user to the database

#4 Buyocat

Buyocat
  • Members
  • PipPipPip
  • Advanced Member
  • 267 posts

Posted 12 June 2006 - 07:34 PM

Could you elaborate on what you mean by automatic? Do you mean you want to check and execute the insert in a single query?
Looking for some easy-to-use tools?  Try these, https://sourceforge....jects/utils-php -- I made them myself.  They're distinct tools which are easy to understand and use.  See some examples uses at http://www.anotherearlymorning.com

#5 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 12 June 2006 - 07:34 PM

You'll want to use Buyocats query and the code will be like this:
//connect to database

$sql = "SELECT user_id FROM user_table WHERE username = '$username' LIMIT 1";
$result = mysql_query($sql);

if(myql_num_rows($result) == 1)
{
    echo "Username in use";
}
else
{
    //continue to register user
}

Also you might want to set the username field to be UNIQUE too.

#6 master82

master82
  • Members
  • PipPipPip
  • Advanced Member
  • 182 posts

Posted 12 June 2006 - 07:38 PM

Thanks, thats just what I needed...

if(myql_num_rows($result) == 1)

sorry for the bad explination -> that is what I was after [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /]




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users