Jump to content

[SOLVED] How to validate username in mysql database already exisits using PHP


obiwan

Recommended Posts

Hey everyone, I set up a registration page and I am trying to figure out how to alert the user that the username they have chosen is already in use I am using the end users email as the username. Please view my code below, I am new to PHP and mysql and would appreciate any help.

 

<?
  // display errors
  ini_set('display_errors',1);
  error_reporting(E_ALL & ~E_NOTICE);
  
  // check passwords match before continuing
  if($user_password != $confirm_password){
  print '<p>Passwords do not match, please correct and try again.</p>';
  print '<span id="regLinks"><a href=../createAccount.htm>Click here to re-enter information</a>';
  exit();
  }?>
      
  <?
 [b]// check if username exists 

        // line below gives me error:Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource//
        if(mysql_num_rows($checkUserName)>=1)
		{
			print '<p>That username already exists, please select another username.</p>';
			exit();
		}[/b]
  
  // if passwords do match update database and display data from db
  if($user_password == $confirm_password)
  {
  print '<br /><span class="title">Congratulations, you have successfully created a profile</span><br />';
  print '<span class="subTitle">The following is your new log In information, you will receive a confirmation email shortly for your records.</span>';
	$db_name = "auth_users";
	$table_name = "site_members";
	$connection = @mysql_connect("myServerInfo","myUserName","myPassWord")
	or die(mysql_error());
	$db = @mysql_select_db($db_name,$connection) or die(mysql_error());
	$checkUserName="SELECT * FROM $table_name WHERE username = '$_POST[user_email]'";
	$userResult = mysql_query($checkUserName) or die(mysql_error());
	$sql ="INSERT INTO $table_name(f_name,l_name,username,password,photoUrl)
	VALUES ('$_POST[f_name]','$_POST[l_name]','$_POST[user_email]',password('$_POST[user_password]'),'$_POST[userPhotoUrl]')";
	$result = @mysql_query($sql,$connection)or die(mysql_error());
	// confirmation email
	$to = "$_POST[user_email]";
	$subject ='New Profile & Log In Information On myUrl';
	// More headers
	$headers .= 'From: <myEmail>' . "\r\n";
	$headers .= 'Cc: myEmail' . "\r\n";
	// body of email //
	$msg = "Your Profile Log In Information on myUrl\n";
	$msg .= "First Name: $_POST[f_name]\n";
	$msg .= "Last Name: $_POST[l_name]\n";
	$msg .= "Username: $_POST[user_email]\n";
	$msg .= "Password: $_POST[user_password]\n";
	mail($to,$subject,$msg,$headers);
  }
?>


 

 

You need to issue a query on the DB for the specific username first.

  $connection = @mysql_connect("myServerInfo","myUserName","myPassWord")
	or die(mysql_error());
	$db = @mysql_select_db($db_name,$connection) or die(mysql_error());
	$checkUserName="SELECT * FROM $table_name WHERE username = '$_POST[user_email]'";
	$userResult = mysql_query($checkUserName) or die(mysql_error());

if (mysql_num_rows($userResult) >=1) 
		{
			print '<p>That username already exists, please select another username.</p>';
			exit();
		} 

Basically you just needed to move your connection and query higher up in your code, so the query is preformed before the check.

 

You need to issue a query on the DB for the specific username first.

  $connection = @mysql_connect("myServerInfo","myUserName","myPassWord")
	or die(mysql_error());
	$db = @mysql_select_db($db_name,$connection) or die(mysql_error());
	$checkUserName="SELECT * FROM $table_name WHERE username = '$_POST[user_email]'";
	$userResult = mysql_query($checkUserName) or die(mysql_error());

if (mysql_num_rows($userResult) >=1) 
		{
			print '<p>That username already exists, please select another username.</p>';
			exit();
		} 

Basically you just needed to move your connection and query higher up in your code, so the query is preformed before the check.

 

 

Ok I will try this right now and see if that does the trick

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.