Click to See Complete Forum and Search --> : some help please


keko2005
03-01-2005, 10:15 AM
hey guys, ok im just learning how to use php, and its a little weird, anyway,im creating a user registration page, im not sure if im even doing it the correct way, but it works. im trying to figure out how to check to see if the username and email address already have been created, because i can create different account with the same usernames and emails. heres the script im using:

<?php



$dbcnx = @mysql_connect("localhost", "", "") ;
if (!$dbcnx) {
print( "<P>Unable to connect to the " . "database server at this time.</P>" );
exit();
}
else{

echo("<p>Connection recieved</P>");
}


if (! @mysql_select_db(" ") )
{
print( "<P>Unable to locate the userlogin " . "database at this time.</P>" );

exit(); }

else{

echo("<p>Connected to user database</P>");
}

$username = $_POST["username"];
$password = $_POST["password"];
$email = $_POST["email"];
$firstName = $_POST["FName"];
$lastName = $_POST["LName"];
$Address = $_POST["address"];
$city = $_POST["city"];
$state = $_POST["state"];



$result= MYSQL_QUERY("INSERT INTO Users (id, username, password, email , FirstName, LastName, Address, city, state)".
"VALUES ('NULL', '$username', '$password', '$email', '$firstName', '$lastName', '$Address', '$city', '$state')");


if(!$result){

echo("<p>login failed</p>");
}

else{

echo("<p>registration successful</p>");
}

the script works when connecting to the database, and everyhting gets stored correctly, i just cant figure out how to write and if statement to check if theres is the same user name or not, can comeone help me out. thnks

NogDog
03-01-2005, 10:33 AM
You probably want to do two things:
Define the user name column in your database to be unique. This will cause MySQL to return an error and not let you insert a record with a duplicate user name.
Add a check before the insert query:

$query = "SELECT `username` FROM `Users` WHERE `username` = '{$_POST['username']}'";
$result = mysql_query($query);
if(mysql_num_rows($result) >= 1)
{
die("<p>ERROR: Username '{$_POST['username']}' already exists. " .
"Please try a different name.</p>");
}