/    Sign up×
Community /Pin to ProfileBookmark

Registration Form – Checking For the Same Username

Hey everyone. I have a registration form. It work perfectly, but I don’t want 2 users with the same username. lol Anyways, how would I do a check to see if the username field is the same as an already taken username, and display a message to show that the username is already taken? Thanks! Here’s the code:

[code=php]<?php
mysql_connect(‘…’, ‘…’, ‘…’) or die(mysql_error());
mysql_select_db (“…”) or die(mysql_error());
if (clean($_GET[‘register’] == ‘true’)) {
registerUser();

}
//Function to sanitize values received from the form. Prevents SQL injection
function clean($str) {
$str = @trim($str);
if(get_magic_quotes_gpc()) {
$str = stripslashes($str);
}
return mysql_real_escape_string($str);
}

function registerUser() {

$username = clean($_POST[‘username’]);
$firstname = clean($_POST[‘firstname’]);
$lastname = clean($_POST[‘lastname’]);
$password = md5( clean($_POST[‘password’]));
$email = clean($_POST[’email’]);
$acename1 = clean($_POST[‘acename1’]);
$acename2 = clean($_POST[‘acename2’]);
$acename3 = clean($_POST[‘acename3’]);
$acename4 = clean($_POST[‘acename4’]);
$acecity1 = clean($_POST[‘acecity1’]);
$acecity2 = clean($_POST[‘acecity2’]);
$acecity3 = clean($_POST[‘acecity3’]);
$acecity4 = clean($_POST[‘acecity4’]);
$gender = clean($_POST[‘gender’]);

$sql = “INSERT INTO users (username, firstname, lastname, password, email, acename1, acename2, acename3, acename4, acecity1, acecity2, acecity3, acecity4, gender) VALUES (‘$username’, ‘$firstname’, ‘$lastname’, ‘$password’, ‘$email’, ‘$acename1’, ‘$acename2’, ‘$acename3’, ‘$acename4’, ‘$acecity1’, ‘$acecity2’, ‘$acecity3’, ‘$acecity4’, ‘$gender’)”;
mysql_query($sql) or die(Mysql_error());

echo “<script type=’text/javascript’>alert(‘Registration successful! Please login in the menu at left.’)</script>”;
}

?>
<html>
<head>
<title>ACE Online Community</title>
<link href=”styles.css” rel=”stylesheet” type=”text/css”>
<script type=”text/javascript”>
function validateFormOnSubmit(theForm) {
var reason = “”;

reason += validateUsername(theForm.username);
reason += validatePassword(theForm.password);
reason += validateEmail(theForm.email);
reason += validatePhone(theForm.phone);
reason += validateEmpty(theForm.from);

if (reason != “”) {
alert(“Some fields need correction:n” + reason);
return false;
}

return true;
}
function validateEmpty(fld) {
var error = “”;

if (fld.value.length == 0) {
fld.style.background = ‘Yellow’;
error = “The required field has not been filled in.n”
} else {
fld.style.background = ‘White’;
}
return error;
}
</script>
</head>
<body bgcolor=”black” link=”red” alink=”red” vlink=”red”>
<font color=”silver”>
<center><h2>ACE Online Community</h2></center>
<?php include(“menu.php”); ?>
<?php include(“registerform.php”); ?>
</font>
</body>
</html>[/code]

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@MindzaiAug 05.2009 — Just query the database against the username and count the number of rows returned. 0 means it doesn't exist, > 0 means it exists.
Copy linkTweet thisAlerts:
@yomamacableguyauthorAug 05.2009 — could you give me a script bit? ^_^;;
Copy linkTweet thisAlerts:
@welshAug 05.2009 — could you give me a script bit? ^_^;;[/QUOTE]
How's this? (you have to integrate it properly into your script but rough idea)
[code=php]
$username = clean($_POST['username']); // Username
$query = "SELECT * FROM users WHERE username = '$username'"; //Query to pull all results from the users table with that username
$result = mysql_query($query) or die(mysql_error());
$num_rows = mysql_num_rows($result); // returns as an int the number of results for query

//checks to see if the returned int is above zero, if it is there is someone with that username!
if($num_rows > 0){
//Do something that tells the user to select a new username
}

[/code]
Copy linkTweet thisAlerts:
@yomamacableguyauthorAug 05.2009 — works like a charm! thanks!!
Copy linkTweet thisAlerts:
@NogDogAug 05.2009 — Note that if you do not use transactions and lock that table during your script, it's possible for two concurrent scripts to still end up with the same user name.
[list=1]
  • [*]script 1 checks to see if "xxxx" is in use, DB says no

  • [*]script 2 checks to see if "xxxx" is in use, DB says no

  • [*]script 1 inserts new user record using "xxxx" as user name

  • [*]script 2 tries to insert new user record using "xxxx" as user name, and depending on whether or not you have defined that field as unique, you either get a failed query error or a duplicate name in the DB
  • [/list]

    You can avoid this by using transactions. Or, you can simply make sure the column has a unique index assigned, then just do your insert query; and if you get an duplicate key error (error 1062 in MySQL) you know you have a duplicate user name and ask the user to try something else.
    ×

    Success!

    Help @yomamacableguy spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.24,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...