Checking with a MySQL database that the username is unique
Posted: Sat Jun 05, 2004 9:48 am
basically, I am making an online php based game, and the registration script requires a username and an email to be inputted. Well there are others, but these are the only ones which I require to be unique.
I have thrown together a little script, but i doesn't work. And I cant think of a way to get around it really.
Now the script is successful in entering the data into the database, but it allows copies of the username!!
Any assistance would be appreciated.
Nick.
I have thrown together a little script, but i doesn't work. And I cant think of a way to get around it really.
Code: Select all
<html>
<?php
//getting all the information from the form that has been sent to us from signup.php
$username = $_POST['username'];
$password = $_POST['password'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$clubname = $_POST['clubname'];
//connect to the MySQL information
include("config.php");
//the stuff to be put in the database
$query = "INSERT into userinfo (firstname,lastname,email,username,password) VALUES ('".$firstname."','".$lastname."','".$email."','".$username."','".$password."')";
$query1 = "INSERT into clubinfo (username,clubname) VALUES ('".$username."','".$clubname."')";
//check that the username is unique and then email is unique
$usernameunique = "Select email from userinfo where username = '".$username."'";
$resultusernameunique = mysql_query($usernameunique, $connect);
$emailunique = "Select username from userinfo where email = '".$email."'";
$resultemailunique = mysql_query($emailunique, $connect);
if (mysql_num_rows($resultusernameunique) == 1)
{
echo 'Username already in use';
}
else
{
if (mysql_num_rows($resultemailunique) == 1)
{
echo 'Email is already registered';
}
else
{
//actually inserting into the database
$result_userinfo = mysql_query($query, $connect);
$result_clubinfo = mysql_query($query1, $connect);
//now that we have told the data to be entered into the database, we can check
$query2 = "SELECT username from userinfo where email = '".$email."' and password = '".$password."'";
$result_test = mysql_query($query2, $connect);
if( mysql_num_rows($result_test) == 0)
{
echo "THERE WAS A FATAL ERROR - PLEASE CONTACT THE WEBMASTER";
}
else
{
echo "Your information has been successfully entered<br />";
}
}
}
/*
NOW ENTERING THE MAILING SECTION
The person who is getting the email is $email
*/
$subject = 'Welcome to CLUBLIFE';
$message = 'Welcome to the onlince nightclub game, CLUBLIFE \n If you did not sign up then someone has signed up using this email address \n Below, you will find your login information \n Username - ".$username." \n Password - ".$password." \n\n Enjoy the Game!! \n Nick';
mail($email,$subject,$message) or die('Your email was sent unsuccessfully');
//close the connections
mysql_close();
?>
</html>Any assistance would be appreciated.
Nick.