I wrote some codes for the form to check to see if username and email address is not taken then allow to it to upload to sql db. Everything seems fine until I decided to test by putting different username with same email address. It seems the function didnt run properly as I want it to be. The bug shows the result: "Your Information is submitted! Thank you! Your Email address is either blank or taken!" I dont know how to force it to recheck before send "Your Information is submitted! Thank You!". Here is my code:

<?
mysql_connect(localhost,$user,$password);
@mysql_select_db($database) or die( "Unable to select database");
$first=$_POST['first'];
$phone=$_POST['phone'];
$mobile=$_POST['mobile'];
$fax=$_POST['fax'];
$email=$_POST['email'];
$web=$_POST['web'];
$query = "INSERT INTO contacts VALUES ('','$first','$phone','$mobile','$fax','$email','$web')";
$query = mysql_query( "SELECT COUNT(*) AS cnt FROM contacts WHERE first = '" . $first . "'" ) and mysql_query( "SELECT COUNT(*) AS cnt FROM contacts WHERE email = '" . $email . "'" ); 
$row = mysql_fetch_object( $query ) or die(mysql_error()); 

if( $row->cnt == 0 ) 
{ 
    print 'Your Information is submitted! Thank you!';
	$query = "INSERT INTO contacts VALUES ('','$first','$phone','$mobile','$fax','$email','$web')";
} 
else 
{ 
    print 'Your Information cant submit because of...'; 
} 
$query = mysql_query( "SELECT COUNT(*) AS cnt FROM contacts WHERE first = '" . $first . "'" ) or die(mysql_error()); 
$row = mysql_fetch_object( $query ) or die(mysql_error()); 

if( $row->cnt == 0 ) 
{ 
$query = "INSERT INTO contacts VALUES ('','$first','$phone','$mobile','$fax','$email','$web')";
} 
else 
{ 
    echo '<br>Your username is either blank or taken!'; 
} 
$query = mysql_query( "SELECT COUNT(*) AS cnt FROM contacts WHERE email = '" . $email . "'" ) or die(mysql_error()); 
$row = mysql_fetch_object( $query ) or die(mysql_error()); 

if( $row->cnt == 0 ) 
{ 
$query = "INSERT INTO contacts VALUES ('','$first','$phone','$mobile','$fax','$email','$web')";
} 
else 
{ 
    print '<br>Your email address is either blank or taken!'; 
} 
mysql_query($query);
mysql_close();
?>

Also of course I already set email and first as unique. Any help will be appreciated. Thanks.

Darthvazor

    instead of two different queries to select the count username and email address..

    try a single query with multiple conditions in the where clause..

    like where (uname ='' and email ='') or (uname ='') or (email='')..

    then u catch those possiblities.. i think..

    jp

      Ah! Your idea works! I got it. _

      But the only problem is, I dont know how to make it specific case to focus on the problem to let user to know which one is already taken. I want it to be easy for users. Can you help me? Thanks

      Btw, here is the modifed code:

      <?
      mysql_connect(localhost,$user,$password);
      @mysql_select_db($database) or die( "Unable to select database");
      $first=$_POST['first'];
      $phone=$_POST['phone'];
      $mobile=$_POST['mobile'];
      $fax=$_POST['fax'];
      $email=$_POST['email'];
      $web=$_POST['web'];
      $query = "INSERT INTO contacts VALUES ('','$first','$phone','$mobile','$fax','$email','$web')";
      $query = mysql_query( "SELECT COUNT(*) AS cnt FROM contacts WHERE first = '" . $first . "' and email = '" . $email . "' or email = '" . $email . "' or first = '" . $first . "'");
      $row = mysql_fetch_object( $query ) or die(mysql_error()); 
      
      if( $row->cnt == 0 ) 
      { 
          print 'Your Information is submitted! Thank you!';
      	$query = "INSERT INTO contacts VALUES ('','$first','$phone','$mobile','$fax','$email','$web')";
      } 
      else 
      { 
          print 'Your Information cant submit because of...'; 
      } 
      mysql_query($query);
      mysql_close();
      ?>
      [code=php]
        14 days later

        oh.. for this u have to do it in same query with three where clause...

        ie.

        if(username and email) #count of query which checks with both usrname and email
        {
        echo 'because of both username and email';
        }
        else if(username) #count of query which checks usrname and email
        {
        echo ' because of username';
        }
        else #count of querywhich checks with email
        {
        echo 'beacause of email';
        }

          Write a Reply...