PDA

View Full Version : How to check if username/email has been taken automatically?


FethrdWlf
02-21-2011, 12:37 AM
I'm trying to allow my users of a certain website to register and login, etc. etc..

I have the register page down, and the script will actually add the info to the MySQL server I have setup, but I want to have some extras added. For example, I don't want people to be able to register with a username that someone else already has.

So pretty much, I have it setup so that the user must enter their desired password twice before they can continue. The problem is, it loads in a blank page (only white), and then displays the errors. I would like it to automatically verify that the passwords match, and then display so on the page. Also, that would be nice to have to make sure that the username and email you entered aren't already in use. How can I do that?

Here's my PHP script for processing the form:
<?php
/*Make the connection:*/
include("openmysql.php");
//---
/*This is the variables for all of the error messages:*/
$rePassMess = "Your passwords do not match. THEY MUST, THEY MUST I TELL YOU!!!";
$tknUserName = "The username you chose has already been taken. Please choose another.";
$tknEmail = "The email you entered is already in use. Please specify another.";
//---
/*Make the variables (The stored information from the form):*/
$userName = $_POST["requsername"];
$passWord = $_POST["reqpassword"];
$repassWord = $_POST["retypepass"];
$email = $_POST["email"];
$fname = $_POST["firstname"];
$lname = $_POST["lastname"];
//---
/*This checks to make sure that everything is correct (No same usernames or emails, and the passwords match)*/
//Passwords
if ($passWord !== $repassWord){
echo $rePassMess,die();
}
//Username
$query = "SELECT username FROM user_info";
$userResult = mysqli_query($cxn,$query);
if ($userName == $userResult){
echo $tknUserName,die();
}


//---
/*Make the INSERT query:*/
$query = "INSERT INTO user_info(username,password,email) VALUES ('$userName','$passWord','$email')";
mysqli_query($cxn,$query);
?>

P.S. The script is not finished. I don't have a way to see if the username or email has already been used.

davidj
03-02-2011, 10:48 AM
you have to query the database after the user keys a user name

I suggest you do this using AJAX

When a user finishes keying a user name and moves to the next field you can invoke an AJAX script which queries the database for the username. This works best in AJAX as the page does not need to be submitted in order to post the form data and on a response of the lookup you can display a tick or cross to indicate the username status.

Here is an example (http://www.w3schools.com/PHP/php_ajax_database.asp)