[SOLVED] Validating unique username
Posted: Wed Jul 21, 2004 9:40 am
I'm trying to create a validation page that allows me to validate the the user has entered data into the registration page (I'm working on ensuring that the formatting is correct and will do so later). I'd also like to have this validation page check for users in the table that have the same username and spit out an error to the user to select a different username if so.
Here's my code:
The error codes for the blank fields work fine, but I can't get an error generated for an existing username. In fact, I don't even get a response on the echo $_SESSION['username']. I don't get it. What am I doing wrong?
Thanks!
Here's my code:
Code: Select all
<?php
//Initialise and error string
$errorString="";
include ("connection/db.php");
$_SESSION['userName']=$_POST["frm_username"];
$formUsername = $_SESSION['userName'];
echo $_SESSION['userName'];
$query = "SELECT username FROM users_tb WHERE username = '".$formUsername."'";
$result = mysql_query ($query);
$numRows = mysql_num_rows ($result);
//Clean and trim the POSTed valueds
//foreach($HTTP_POST_VARS as $varname => $value)
// $formVars[$varname]= trim(clean($value,50));
//Clean and trim the POSTed valueds
foreach($HTTP_POST_VARS as $varname => $value)
$formVars[$varname]= $value;
//Validate the first name
if (empty($formVars["frm_firstName"]))
//First name cannot be a null string
$errorString.=
"\n<br />The first name field cannot be blank.";
//Validate the last name
if (empty($formVars["frm_lastName"]))
//Last name cannot be a null string
$errorString.=
"\n<br />The last name field cannot be blank.";
//Validate the address1
if (empty($formVars["frm_address1"]))
//First name cannot be a null string
$errorString.=
"\n<br />The address field cannot be blank.";
//Validate the city
if (empty($formVars["frm_city"]))
//First name cannot be a null string
$errorString.=
"\n<br />The city field cannot be blank.";
//Validate the state
if (empty($formVars["frm_state"]))
//First name cannot be a null string
$errorString.=
"\n<br />The state field cannot be blank.";
//Validate the postal
if (empty($formVars["frm_postal"]))
//First name cannot be a null string
$errorString.=
"\n<br />The postal field cannot be blank.";
//Validate the username
if (empty($formVars["frm_username"]))
//First name cannot be a null string
$errorString.=
"\n<br />The username field cannot be blank.";
// Check to see if username exists already
if ($numRows == ('1'));
"\n<br />The username you have selected already exists. Please try another username.";
//Validate the password
if (empty($formVars["frm_password"]))
//First name cannot be a null string
$errorString.=
"\n<br />The password field cannot be blank.";
//Validate the email
if (empty($formVars["frm_emailAddress"]))
//First name cannot be a null string
$errorString.=
"\n<br />The email field cannot be blank.";
//Validate the email
if (empty($formVars["frm_country"]))
//First name cannot be a null string
$errorString.=
"\n<br />The country field cannot be blank.";
//Now the script has finished the validation, check if there were any errors
if (!empty($errorString))
{
//There are errors show them and exit.
?>Thanks!