Page 1 of 1

Help - Trying to avoid duplicate entries [Solved]

Posted: Wed Jan 14, 2004 11:20 am
by johnperkins21
I am trying to set up a database with user info. However, do not want to add a user using duplicate emails. I am a complete noob and have no idea why this code is not working.

Code: Select all

<?php
$link = mysql_connect ( $hostname, $username, $password );

mysql_select_db ( $db )
		or die ( "Couldn't open the database. " . mysql_error() );
		
$duplicate_email = mysql_query("SELECT * FROM TroupeInfo WHERE Email='$_POST['Email']'");

if ( mysql_num_rows( $duplicate_email ) < 1 ) {

	$query = "INSERT INTO TroupeInfo ( CreatedOn, FirstName, LastName, Address1, Address2, City, State, Postal, Telephone, Email ) 
           	VALUES( now() , '".$_POST['FName']."' , '".$_POST['LName']."' , '".$_POST['Address1']."' , '".$_POST['Address2']."' , '".$_POST['City']."' , '".$_POST['State']."' , '".$_POST['Zip']."' , '".$_POST['Telephone']."' , '".$_POST['Email']."' )"; 

	mysql_query( $query, $link ) 
		or die (" Couldn't add data to the table: " . mysql_error() );
		
	else {
		$string_message = 'The email address ' . $_POST['Email'] . ' already exists in our database. Thank you for your interest.';
		echo $string_message;
	}
}
	
mysql_close( $link ); 

?>

When I comment out the check, I can add to the database just fine. With the check, I cannot enter ANY information to the dbase. I am confused. Any help, or a push in the right direction would be greatly appreciated.

Thank you for your help.
John

Solved

Posted: Wed Jan 14, 2004 1:28 pm
by johnperkins21
I'm an idiot. My code was wrong.

Changed code:

Code: Select all

<?php
if ( $num_rows < 1 ) {

	$query = "INSERT INTO TroupeInfo ( CreatedOn, FirstName, LastName, Address1, Address2, City, State, Postal, Telephone, Email ) 
           	VALUES( now() , '".$_POST['FName']."' , '".$_POST['LName']."' , '".$_POST['Address1']."' , '".$_POST['Address2']."' , '".$_POST['City']."' , '".$_POST['State']."' , '".$_POST['Zip']."' , '".$_POST['Telephone']."' , '".$_POST['Email']."' )"; 

	mysql_query( $query, $link ) 
		or die ( "Couldn't add data to the table: " . mysql_error() );
		
}		
	else {
		$string_message = 'The email address ' . $_POST['Email'] . ' already exists in our database. Thank you for your interest.';
		echo $string_message;
	}
?>
I hade my { } in all disarray. Perils of noobdom.