Hi Guys Im developing an application and im inserting users to my database but i want to make sure that a username can only be one, no usernames are the same.. How wud i do that in PHP..

Recommended Answers

All 2 Replies

before you do the insert, query the table for that username to check if it already exists, and if it does, return a "username already taken" error message.

kinda like this:

$username = mysql_real_escape_string($_POST['username']);

//check if username exists
$check_user = mysql_query("SELECT * FROM `users` WHERE username = '".$username."'") or die(mysql_error());

if(mysql_num_rows($check_user) > 0){
 //user exists do some code
 echo "user already exists";
 die();
}else{
 //username is available
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.