Page 1 of 1

[SOLVED] very basic if/else problems

Posted: Mon Apr 04, 2005 10:25 am
by imstupid
I'm missing something so basic, but I can't figure it out. I think I need sleep. Basically, I'm adding a player to a database, and if the email address entered (via html form) already exists in the database, the code prints the message 'the player already exists.' However if the email address doesn't exist, it adds the player's info to the database. The code can find the duplicate email fine and puts out the alert message, but if it is a new email address, it won't run the code to after the else statement. I know there is probably a million other ways to do this more efficiently, but I stink at php. Thanks in advance.

code:

Code: Select all

<?PHP

$email = $_REQUEST['email'] ;

$hd = mysql_connect("host", "username", "password")
		or die ("Unable to connect");
		
	
$result = mysql_select_db ("databasename", $hd)
          or die ("Unable to select database");


$getoldemail = mysql_query("SELECT * FROM table_1 WHERE email='$email'") 
 	or die(mysql_error()); 
$oldemail = mysql_fetch_array( $getoldemail )
	or die(mysql_error());	  
		  
$oldemailarray = $oldemail['email'] ;	
 
	if ($oldemailarray == $email) {
		echo "<p class='style1'>ERROR:</p>&nbsp;&nbsp;Player already entered.<br><br>";
		echo "<a href='javascript:history.back(1);'>Back</a>";
		
		mysql_close($hd);
	}
	
	else {
                $sql = "SELECT * FROM table_2 WHERE email='$email'"; 
	        $result = mysql_query($sql) or die ("Couldn't get reults.");
	        $count = mysql_num_rows($result); 
	
	        if ($count == 1 ) {
	             my code
	             mysql_close($hd);
		   }
	        else if ($count == 0) { 
	             my code
	             mysql_close($hd);
		   }
         } 
?>

Posted: Mon Apr 04, 2005 10:32 am
by Burrito
since it will only return one row (if the email is there...try doing it like this:

Code: Select all

$getemail = mysql_query("select * from myTable where email = '".$_POSt['email']."'")
   or die(mysql_error());

if($gtemail = mysql_fetch_assoc($getemail)){
  $message = "email alraedy taken"
}else{
  mysql_query("insert into blah blah blah....")
     or die(mysql_error());
}

if(isset($message)){
  echo $message;
  exit();
}
that will only die if a row is returned...(an email exists on the database).

Posted: Mon Apr 04, 2005 10:48 am
by feyd
if you made the 'email' field a unique, trying to insert an existing one would fail.

Posted: Mon Apr 04, 2005 11:49 am
by imstupid
although I am a taco man, I have to give it up to the burrito. thanks everyone, it works.