Current location: Hot Scripts Forums » Programming Languages » PHP » Query for existing users and emails


Query for existing users and emails

Reply
  #1 (permalink)  
Old 02-20-04, 04:24 PM
mdhall's Avatar
mdhall mdhall is offline
Aspiring Coder
 
Join Date: Oct 2003
Posts: 510
Thanks: 1
Thanked 1 Time in 1 Post
Post Query for existing users and emails

I am setting up a form to let people join a site. The script checks for missing form fields, sees if the username and email are already used, and inserts the new members data into a table. The following script works, including the missing form data and the insert, except that it isn't giving the error message when a username or email address is already being used. It simply adds the data into the table. I've seen this type of usage before in other scripts, but apparently I added it here incorrectly. Any suggestions?

Heres the script....

PHP Code:

$ip=$_SERVER['REMOTE_ADDR'];
$first_name=$_POST['first_name'];
$last_name=$_POST['last_name'];
$email=$_POST['email'];
$user=$_POST['user'];
$pass=$_POST['pass'];
                
 if((!
$first_name) || (!last_name) || (!$email) || (!$user) || (!$pass)){
 echo 
'You did not submit the following required information. Use your browsers "back" button to complete your form. <br />';
 if(!
$first_name){
  echo 
"Your first name is required. <br />";
 }
 if(!
$last_name){
 echo 
"Your last name is required. <br />";
 }
 if(!
$email){
 echo 
"Your email is required. <br />";
 }
 if(!
$user){
 echo 
"A username is required. <br />";
 }
 if(!
$pass){
 echo 
"A password is required. <br />";
 }
 }
 else
 {
              
include(
"dbinfo.inc.php");
$conn mysql_connect("$location","$username","$password");
if (!
$conn) die ("Could not connect MySQL");
mysql_select_db($database,$conn) or die ("Could not open database");
$user_check = ("SELECT user FROM users");
$user_result=mysql_query($user_check);
              
while(
$user_taken=mysql_fetch_array($user_result))
{
$username_taken=$user_taken[user];
}
if (
$username_taken)
{
echo 
"That username is already taken. Please select another username.";
}
$email_check = ("SELECT email FROM users");
$email_result=mysql_query($email_check);
while(
$email_taken=mysql_fetch_array($email_result))
{
$email_used=$email_taken[email];
}
if (
$email_used)
{
echo 
"That email is already taken. Please select another email address.";
}
$query "INSERT INTO users VALUES('','$first_name','$last_name','$email','$user','$pass','$ip')";
$result=mysql_query($query);
mysql_close();
               
header ("Location: index.php");
   } 
Reply With Quote
  #2 (permalink)  
Old 02-20-04, 04:55 PM
YourPHPPro's Avatar
YourPHPPro YourPHPPro is offline
Community VIP
 
Join Date: Aug 2003
Posts: 430
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by mdhall
The following script works, including the missing form data and the insert, except that it isn't giving the error message when a username or email address is already being used.
Instead of getting all users from the database, you could try something like this:

Code:
select count(*) as Count from users where user='$username'
That way you only need to see if the number that is returned is >0 - if so, then you know there is an existing username.

The same thing can be done with the email. It could also be arranged like:

Code:
select count(*) as Count from users where user='$username' OR email='$email'
That would check for either a username conflict or an email conflict. If Count>0 then send visitor to generic error page.
Reply With Quote
  #3 (permalink)  
Old 02-20-04, 05:09 PM
YourPHPPro's Avatar
YourPHPPro YourPHPPro is offline
Community VIP
 
Join Date: Aug 2003
Posts: 430
Thanks: 0
Thanked 0 Times in 0 Posts
Here is some code that might help you out:

Code:
mysql_select_db($database,$conn) or die ("Could not open database");
$combined_check = mysql_fetch_assoc(mysql_query("select count(*) as Count from users where user='$user' or email='$email'"));

if ($combined_check["Count"]>0) {
	header ("Location: error_page.php");
	quit;
}

// Results have been checked, now lets insert data into database
Reply With Quote
  #4 (permalink)  
Old 02-20-04, 05:18 PM
mdhall's Avatar
mdhall mdhall is offline
Aspiring Coder
 
Join Date: Oct 2003
Posts: 510
Thanks: 1
Thanked 1 Time in 1 Post
I tried this, and It still does the same thing, i.e., add the info even though its already taken.

$combined_check = mysql_fetch_assoc(mysql_query("select count(*) as Count from users where
user='$user' or email='$email'"));
if ($combined_check["Count"]>0) {
echo "That username or password is in use";
quit;
}


I have had trouble getting sessions to work on members-type pages I set up. Basically, I want a system for viewers to become members of a site, so that only members can post material, however anyone can view what is there already. If you have any suggestions or sample scripts I could try out, I would really appreciate it.

Last edited by mdhall; 02-20-04 at 05:21 PM.
Reply With Quote
  #5 (permalink)  
Old 02-20-04, 05:21 PM
YourPHPPro's Avatar
YourPHPPro YourPHPPro is offline
Community VIP
 
Join Date: Aug 2003
Posts: 430
Thanks: 0
Thanked 0 Times in 0 Posts
Can you edit your previous post to reflect that latest code changes that you made ?
Reply With Quote
  #6 (permalink)  
Old 02-20-04, 05:24 PM
mdhall's Avatar
mdhall mdhall is offline
Aspiring Coder
 
Join Date: Oct 2003
Posts: 510
Thanks: 1
Thanked 1 Time in 1 Post
Quote:
Originally Posted by YourPHPPro
Can you edit your previous post to reflect that latest code changes that you made ?
Heres the full code i tried...

PHP Code:

 
$ip
=$_SERVER['REMOTE_ADDR'];
$first_name=$_POST['first_name'];
$last_name=$_POST['last_name'];
$email=$_POST['email'];
$user=$_POST['user'];
$pass=$_POST['pass'];
                
 if((!
$first_name) || (!last_name) || (!$email) || (!$user) || (!$pass)){
 echo 
'You did not submit the following required information. Use your browsers "back" button to complete your form. <br />';
 if(!
$first_name){
  echo 
"Your first name is required. <br />";
 }
  if(!
$last_name){
  echo 
"Your last name is required. <br />";
 }
 if(!
$email){
  echo 
"Your email is required. <br />";
 }
 if(!
$user){
  echo 
"A username is required. <br />";
 }
if(!
$pass){
  echo 
"A password is required. <br />";
 }
 }
else
{
              
include(
"dbinfo.inc.php");
$conn mysql_connect("$location","$username","$password");
mysql_select_db($database,$conn) or die ("Could not open database");
$combined_check mysql_fetch_assoc(mysql_query("select count(*) as Count from users where user='$user' or email='$email'"));
if (
$combined_check["Count"]>0) {
 echo 
"That username or password is in use";
 
quit;
}

$query "INSERT INTO users VALUES('','$first_name','$last_name','$email','$user','$pass','$ip')";
$result=mysql_query($query);
mysql_close();
               
header ("Location: index.php");
   } 
Reply With Quote
  #7 (permalink)  
Old 02-21-04, 12:45 PM
NeverMind's Avatar
NeverMind NeverMind is offline
Community VIP
 
Join Date: Aug 2003
Location: K.S.A
Posts: 2,257
Thanks: 0
Thanked 2 Times in 1 Post
hmm ..
why are you using quit; ? and what is it exactly?
what I know is die; and exit; are the ones that terminate script execution!!

anyway, I suggest that you use the or die() part in your mysql_query to check if the query was excuted or not ..
so something like this might help:
PHP Code:

$check mysql_query("select count(*) as Count from users where user='$user' or email='$email'")or die(mysql_error());


$combined_check mysql_fetch_assoc($check); 
__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]
Reply With Quote
  #8 (permalink)  
Old 02-21-04, 01:56 PM
mdhall's Avatar
mdhall mdhall is offline
Aspiring Coder
 
Join Date: Oct 2003
Posts: 510
Thanks: 1
Thanked 1 Time in 1 Post
Thnx Nevermind..that did the trick.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
need help with mysql query achieve PHP 0 02-20-04 08:05 AM
Emailing from query results Aaronn PHP 1 11-08-03 11:45 PM


All times are GMT -5. The time now is 09:04 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.