Check if user Exists function
Posted: Wed Jan 29, 2003 10:16 pm
Hey guys!
I'm on week 1 of PHP and MySQL learning and I need some basic help.
I'm trying to code a function that checks if a username is already in the database and if it is, return an error. If it is not, then it would proceed to add the user along with their other info. Here is what I have:
It seems to work fine except IF a user name DOES already exist, $result becomes undefined and it echos '&Confirmation=An Error has occured. Please contact site admin&'.
This result is harmless but is it good programming practice to let this happen? Can someone suggest a better way to code this and at the same time explain why?
Please Advise.
Thanks in advance you guys!
I'm on week 1 of PHP and MySQL learning and I need some basic help.
I'm trying to code a function that checks if a username is already in the database and if it is, return an error. If it is not, then it would proceed to add the user along with their other info. Here is what I have:
Code: Select all
<?php
$connection = mysql_connect ("", "", "");
if ($connection == false){
echo mysql_errno().": ".mysql_error().
exit;
}
$check_if_user = "SELECT * FROM user_info WHERE UserName='$UserName'";
$check = mysql_db_query ("incomming", $check_if_user);
if(mysql_affected_rows() == 1) {
echo"&invalid=User name already in use. Please choose a different user name.&";
}
else {
$query = "INSERT INTO user_info VALUES ('$FirstName', '$LastName','$Address1', '$Address2', '$City', '$State', '$Zip', '$Phone', '$Fax', '$Email','$Website', '$ResaleID', '$AccountNum', '$UserName', '$Password', '$Date')";
$result = mysql_db_query ("incomming", $query);
}
if ($result){
echo"&Confirmation=Thank you $FirstName. You have been registered successfully. An e-mail will be sent to you shortly.&";
}
else{
echo"&Confirmation=An Error has occured. Please contact site admin&";
mysql_close ();
}
?>This result is harmless but is it good programming practice to let this happen? Can someone suggest a better way to code this and at the same time explain why?
Please Advise.
Thanks in advance you guys!