[SOLVED] very basic if/else problems

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
imstupid
Forum Commoner
Posts: 84
Joined: Fri Feb 18, 2005 1:24 pm

[SOLVED] very basic if/else problems

Post 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);
		   }
         } 
?>
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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).
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

if you made the 'email' field a unique, trying to insert an existing one would fail.
imstupid
Forum Commoner
Posts: 84
Joined: Fri Feb 18, 2005 1:24 pm

Post by imstupid »

although I am a taco man, I have to give it up to the burrito. thanks everyone, it works.
Post Reply