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>