Check if user Exists function

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Sleeve
Forum Newbie
Posts: 17
Joined: Wed Jan 22, 2003 12:39 am

Check if user Exists function

Post by Sleeve »

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:

Code: Select all

<?php

  $connection = mysql_connect ("", "", "");
  if ($connection == false)&#123;
    echo mysql_errno().": ".mysql_error().
    exit;
  &#125;   
  $check_if_user = "SELECT * FROM user_info WHERE UserName='$UserName'";
  $check = mysql_db_query ("incomming", $check_if_user);
  if(mysql_affected_rows() == 1) &#123;
		echo"&invalid=User name already in use.  Please choose a different user name.&";
	&#125;
	else &#123;
		$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);
	&#125;
  
  if ($result)&#123;
    echo"&Confirmation=Thank you $FirstName. You have been registered successfully. An e-mail will be sent to you shortly.&";
  &#125;
  else&#123;
    echo"&Confirmation=An Error has occured.  Please contact site admin&";
  
  mysql_close ();
  &#125;
?>
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!
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Hi have a go with this code. Some of the changes I made were:
  • Using mysql_select_db() and mysql_query() instead of mysql_db_query() which shouldn't be used.
  • Using mysql_num_rows() instead of mysql_affected_rows() to test if the username already exists because mysql_affected_rows() does not work on SELECT statements.
  • Putting the if statement that determines whether the INSERT was a success or failure inside the part of the other if statement that actually INSERTS the record because then it won't run unless the INSERT does.

Code: Select all

<?php 

@$connection = mysql_connect('', '', '') or die(mysql_errno().': '.mysql_error()); 
@mysql_select_db('incomming') or die(mysql_error());

$check_if_user = "SELECT * FROM user_info WHERE UserName='$UserName'"; 
$check = mysql_query($check_if_user) or die(mysql_error().'<p>'.$check_if_user.'</p>');

if (mysql_num_rows($check) > 0) {
	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')"; 
	mysql_query($query) or die(mysql_error().'<p>'.$query.'</p>');
	if (mysql_affected_rows() == 1){ 
		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();
?>
Mac
Sleeve
Forum Newbie
Posts: 17
Joined: Wed Jan 22, 2003 12:39 am

Post by Sleeve »

Beautiful!

I had NO IDEA I could use If else statements inside of if else statements! It makes perfect sense to me now.

thanks a bunch for the help!
evilcoder
Forum Contributor
Posts: 345
Joined: Tue Dec 17, 2002 5:37 am
Location: Sydney, Australia

Post by evilcoder »

Sleeve, of coarse you can, but i'll give you both a warning and advice.

When creating if statements, i find it best to add the braces as soon as you create it, that way you don't have to go over it and find out where you have to escape your statements. I recently forgot to do it and it took me near 20 minutes to escape about 12 IF statements in a function.

I will also tell you one more thing... Its about presentation. When coding in PHP it is necessary that you code neatly eg:

Code: Select all

<?php

if ( $Something <> "A Value" )
{
   echo $Something . " Is not the real value";
}
else
{
   echo $Something . " Is the correct Answer";
}

?>
That's a great example of neat coding. The same also applies for multi-level statements eg:

Code: Select all

<?php
if ( $Something == "Value" )
{
   if ( $Anotherthing <> "Another Value" )
   {
      echo "Sorry but " . $Anotherthing . "is not right";
   }
   else
   {
      echo "Yep " . $Anotherthing . "is right";
   }
else
{
   echo "Sorry, but " . $Something . "is not the right value";
}

?>

Learning PHP tidiness is a great help for yourself and others.
?>
Post Reply