Checking User Information

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
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Checking User Information

Post by AliasBDI »

I have an update page which takes information from a form. It first gathers the variables and then checks the database to make sure the person did not change there username to one that is already in the database.

I did a mere IF statement to do the checking. My problem is, that it is finding the very record of the person who is doing the updating and says that it already exists. Any ideas on how to pull this off more effectively?
microthick
Forum Regular
Posts: 543
Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC

Re: Checking User Information

Post by microthick »

AliasBDI wrote:I have an update page which takes information from a form. It first gathers the variables and then checks the database to make sure the person did not change there username to one that is already in the database.

I did a mere IF statement to do the checking. My problem is, that it is finding the very record of the person who is doing the updating and says that it already exists. Any ideas on how to pull this off more effectively?
In the form, create a hidden field that contains the username, called originalUsername or something.

Then, only run the if statement if $username != $originalUsername.
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

crap...

Post by AliasBDI »

Crap. I'm a newbie. I a bit confused about where to put it... It makes sense though. Here is my code:

Code: Select all

$sql_email_check = mysql_query("SELECT email_address FROM users WHERE email_address='$email_address'");
 $sql_username_check = mysql_query("SELECT username FROM users WHERE username='$username'");
 $sql_dl_check = mysql_query("SELECT username FROM users WHERE dl='$dl'");

 $email_check = mysql_num_rows($sql_email_check);
 $username_check = mysql_num_rows($sql_username_check);
 $dl_check = mysql_num_rows($sql_dl_check);

 if(($email_check > 0) || ($username_check > 0) || ($dl_check > 0)){
 	echo "Please fix the following errors: <br />";
 	if($email_check > 0)&#123;
 		echo "<strong>Your <b>email address</b> has already been registered by another member in our database. Please submit a different Email address!<br />";
 		unset($email_address);
 	&#125;
 	if($username_check > 0)&#123;
 		echo "The <b>username</b> you have selected has already been registered by another member in our database. Please choose a different Username!<br />";
 		unset($username);
 	&#125;
 	if($dl_check > 0)&#123;
 		echo "The <b>driver's license</b> you have selected has already been registered by another member in our database. Please choose a different License number or contact our customer support in order to reopen your account.!<br />";
 		unset($username);
 	&#125;
 	echo "Please go BACK and try again."; // Show the form again!
 	exit();  // exit the script so that we do not create this account!
 &#125;
microthick
Forum Regular
Posts: 543
Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC

Post by microthick »

You could probably just add a simple check here:

Code: Select all

$sql_email_check = mysql_query("SELECT email_address FROM users WHERE email_address='$email_address'"); 
$sql_username_check = mysql_query("SELECT username FROM users WHERE username='$username'"); 
$sql_dl_check = mysql_query("SELECT username FROM users WHERE dl='$dl'"); 

$email_check = mysql_num_rows($sql_email_check);

// If the updated username is the same as the original
// username, assume that the updated username is safe to use.
// Otherwise, check how many people are using this username.
if ($username == $oldusername) &#123;
     $username_check = 0;
&#125; else &#123;
     $username_check = mysql_num_rows($sql_username_check);
&#125;
$dl_check = mysql_num_rows($sql_dl_check); 

if(($email_check > 0) || ($username_check > 0) || ($dl_check > 0))&#123; 
   echo "Please fix the following errors: <br />"; 
   if($email_check > 0)&#123; 
      echo "<strong>Your <b>email address</b> has already been registered by another member in our database. Please submit a different Email address!<br />"; 
      unset($email_address); 
   &#125; 
   if($username_check > 0)&#123; 
      echo "The <b>username</b> you have selected has already been registered by another member in our database. Please choose a different Username!<br />"; 
      unset($username); 
   &#125; 
   if($dl_check > 0)&#123; 
      echo "The <b>driver's license</b> you have selected has already been registered by another member in our database. Please choose a different License number or contact our customer support in order to reopen your account.!<br />"; 
      unset($username); 
   &#125; 
   echo "Please go BACK and try again."; // Show the form again! 
   exit();  // exit the script so that we do not create this account! 
&#125;
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

thanks.

Post by AliasBDI »

Works fantastic...I added some more variables to check and it is working exactly how I need it.
microthick
Forum Regular
Posts: 543
Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC

Post by microthick »

Awesome.
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

another question

Post by AliasBDI »

what if I wanted to let the field be empty. So it will leave it alone if the field is left blank.
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

Code: Select all

<?php
if (!empty ($_POST['new_username'])) {
    //update username
}
?>
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

hmmm

Post by AliasBDI »

vigge89--

Where does this go. I'm not fimiliar with that code..

thanks.
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

oh, that was just an example, i haven't looked at your code yet...
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

try placing it between this code:

Code: Select all

<?php
   if($username_check > 0){
      echo "The <b>username</b> you have selected has already been registered by another member in our database. Please choose a different Username!<br />";
      unset($username); 
?>
IE:

Code: Select all

<?php
if (!empty ($_POST['new_username'])) {

   if($username_check > 0){
      echo "The <b>username</b> you have selected has already been registered by another member in our database. Please choose a different Username!<br />";
      unset($username); 

} 
?>
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

vigge89

Post by AliasBDI »

My code has changed a bit since that last time I posted it. The block which we are dealing with here now looks like this:

Code: Select all

$sql_email_check = mysql_query("SELECT email_address FROM users WHERE email_address='$email_address'");
 $sql_username_check = mysql_query("SELECT username FROM users WHERE username='$username'");
 $sql_dl_check = mysql_query("SELECT username FROM users WHERE dl='$dl'");

 $email_check = mysql_num_rows($sql_email_check);
 $username_check = mysql_num_rows($sql_username_check);
 $dl_check = mysql_num_rows($sql_dl_check);

 if(($email_check > 0) || ($username_check > 0) || ($dl_check > 0))&#123;
 	echo "Please fix the following errors: <br />";
 	if($email_check > 0)&#123;
 		echo "<strong>Your <b>email address</b> has already been registered by another member in our database. Please submit a different Email address!<br />";
 		unset($email_address);
 	&#125;
 	if($username_check > 0)&#123;
 		echo "The <b>username</b> you have selected has already been registered by another member in our database. Please choose a different Username!<br />";
 		unset($username);
 	&#125;
 	if($dl_check > 0)&#123;
 		echo "The <b>driver's license</b> you have selected has already been registered by another member in our database. Please choose a different License number or contact our customer support in order to reopen your account.!<br />";
 		unset($username);
 	&#125;
 	echo "Please go BACK and try again."; // Show the form again!
 	exit();  // exit the script so that we do not create this account!
 &#125;
I have more than one checker, so I get a bit confused on how to pull it off without messing up what I have - which I've done already!

Thanks for your help.[/php_man]
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

viggie89

Post by AliasBDI »

I tried this but it did not work. Could my code be wrong?

Code: Select all

if (!empty ($_POST&#1111;'new_username']) || ($_POST&#1111;'new_dl']) &#123;
	if(($username_check > 0) || ($dl_check > 0))&#123;
 		echo "Please fix the following errors: <br />";
 		if($username_check > 0)&#123;
 			echo "The <b>username</b> you have selected has already been registered by another member in our database. Please choose a different Username!<br />";
 			unset($username);
 		&#125;
 		if($dl_check > 0)&#123;
 			echo "The <b>driver's license</b> you have selected has already been registered by another member in our database. Please choose a different License number or contact our customer support in order to reopen your account.!<br />";
 			unset($username);
 		&#125;
 		echo "Please go BACK and try again."; // Show the form again!
 		exit();  // exit the script so that we do not create this account!
 		&#125;
	&#125;
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

hmmm...

Post by AliasBDI »

Anyone know?
Post Reply