View Full Version : Check for Duplicates?

12-04-2010, 10:27 AM
Hi there,

Trying to create a basic script to email across a users email address. That part was working. I also want it to check the database to see if their address is already in the database - though now nothing seems to be working. I'm sure it's just a small error.

Appreciate any help.

if ($_POST['email']!=""){
$email = mysql_real_escape_string($_POST['email']);
$result = mysql_query("SELECT * FROM addresses");
$num_rows = mysql_num_rows($result);
$sql = mysql_query("SELECT * FROM addresses WHERE email='$email'");
$numRow = mysql_num_rows($sql);

//Section to check for valid email address
if ($numRow>0){

else(false !== filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){

$sql_insert=mysql_query("INSERT INTO addresses (email, dateTime) VALUES('$email',now())") or die (mysql_error());

$to = "example@gmail.com";
$subj = "Newsletter Subscriber";
$headers = "From:info@test.com\r\n";
$headers .= "Content-type: text/html\r\n";
$content = "Hi,<br><br>A new address ($email) has been added to the newsletter database.<br><br>There is now a total of <strong>$num_rows</strong> email addresses in the database.<br><br>Thanks,<br><br>The Computer<br><br><span style='font-size: 10px; color: #777777;'>This is an automated message, you do not need to reply or take any type of action. Do not reply to this message - no one will receive it!</span>";
mail($to, $subj, $content, $headers);

//Message to Subscriber
$toSub = $_POST['email'];
$subjSub = "Newsletter";
$headersSub = "From:info@test.com\r\n";
$headersSub .= "Content-type: text/html\r\n";
$contentSub = "Hi,<br><br>Thank you for subscribing to the Newsletter.<br><br>You will receiving your first newsletter from us soon.<br><br>Thanks,<br><br>Hello World";
mail($toSub, $subjSub, $contentSub, $headersSub);

12-04-2010, 04:08 PM
if ($numRow>0){

Essentially, if they exist in the database you terminate the script. Chances are, you want to replace that exit() call with the $sql_insert a few lines below it. That would change it to only insert the record if it doesn't exist, and will still process the mailing itself.
If I understand your intentions here.

You can also dump the mysql call for $result and $num_rows, neither appear to be in use anywhere.