Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Check to see if username is already being used

$
0
0
I am trying to check my database for a username when a new user tries to create an account. If the username chosen already exists I want to display an error message stating this. Here is the code I cam up with but it does not work. Also, I'd like to check the same for email addresses. I do not want a user to have multiple usernames on the same email address.

$sql = "SELECT COUNT(*) FROM user WHERE user_name = '{$_POST['user_name']}'";
sql_result = mysql_query($sql);
 
if (mysql_result($sql_result, 0) > 0)
{
Echo "This username is already in use.Please choose another.";
showForm(null);
exit();
}
else //insert into database
{
//do insert
}



I've also tried this code:
$sql = mysql_query("SELECT * FROM user WHERE user_name='$UserName'");
$result = mysql_num_rows($sql);

if($result !=="0"){
   
echo "The user name you have chosen already exists! Please enter a different user name";
exit();
}
else //insert into database
{
//do insert
}



Any help would be appreciated. Also, would the code to check the email be added to the SQL statement within the code where I have it??

Viewing all articles
Browse latest Browse all 51036

Trending Articles