Jump to content

Archived

This topic is now archived and is closed to further replies.

master82

Username Check

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Could you elaborate on what you mean by automatic? Do you mean you want to check and execute the insert in a single query?

Share this post


Link to post
Share on other sites
You'll want to use Buyocats query and the code will be like this:
[code]//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
}[/code]

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

Share this post


Link to post
Share on other sites
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\" /]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.