Hi all,

I have written the following script and wish to add in a part to make sure the email is not a duplicate.

Im a bit unsure as when i added in an sql clause to check_num_rows after the "isset" it just ignored the check.

Heres the code

<?php
session_start();
include "connect.php";
if(isset($_POST['register'])) 
{ 

// I TRIED ADDING AN SQL STATEMENT HERE BUT IT WAS JUST IGNORED///

if (isValidEmail($_POST['email'])){
	
		$name ='';
		$email='';
		$nationality='';
    $name = $_POST['name'];
	$email = $_POST['email'];
	$nationality = $_POST['country'];
	$SQL = "INSERT INTO tbl_beta (name, email, nationality) VALUES ('$name', '$email', '$nationality')";
		$result = mysql_db_query($db,$SQL,$cid);
    echo "Thanks for registering " . $name;
    echo "<br>We will be in touch shortly."; 
	$_SESSION['betaemail'] = $email;
	include "mail.php";
	exit();
 }else {echo "please enter a valid email address";}
}
?>

The first thing that comes to mind is that $_POST['register'] is not set. Have you tried anything to verify a value is being posted?

ie.

if(isset($_POST['register'])) 
{ 
     echo 'IS SET'; die; 
} else {
     echo 'IS NOT SET'; die; }

Hi,

Thanks for the response.

I have much of the page out which includes the form itself that hold the action of php self. Im fine with what you have suggested, its just this addition of checking for duplicates

ok here goes,

Ive removed the code inserting into the database and added a header redirect on success which then uses the insert statement from session variables.

The code is as below, but its still ignoring it

<?php
session_start();
include "connect.php";
		$name = $_SESSION['name'];
		$email = $_SESSION['email'];
		$nationality = $_SESSION['country'];
echo $query = "SELECT * FROM tbl_beta WHERE email = $email";
$result=mysql_query($query);
$unique = mysql_num_rows($result);
if($unique > 1){
	echo "Sorry, That email has already been registered in out database.  Please contact support at support@somewhere.com";
	exit();
	}else{
	$SQL = "INSERT INTO tbl_beta (name, email, nationality) VALUES ('$name', '$email', '$nationality')";
		$result = mysql_db_query($db,$SQL,$cid);
    echo "Thanks for registering " . $name;
    echo "<br>We will be in touch shortly."; 
	$_SESSION['betaemail'] = $email;
	include "mail.php";
	}
	?>

Change your if statement to be greater than zero. Right now the email address would have to be registered twice to get a response from your if statement.

if($unique > 0){
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.