Find Duplicates Not Working?

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.

<?php
if ($_POST['email']!=""){
	include('connection.php');
	$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){
	exit();
	}

	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\
";
	$headers .= "Content-type: text/html\\r\
";
	$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\
";
	$headersSub .= "Content-type: text/html\\r\
";
	$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);
	}
}
?>

this could be a good opportunity to enhance your debugging skills with some basic debugging

Hi, I have now placed error_reporting(E_ALL); at the top of the PHP opening tag but just still displays a plain white page.

Hello, ok worked out the problem. I used else if instead of just else. I thought it was the other way around. You learn something everyday! :slight_smile:

Professor Oak of debugging over there forgot to tell ya, during development it’s definitely a good idea to put an error message with those dies/exits for situations JUST like that.

So…

Instead of:

 die();
## or
exit(); 

Do

 die('Failed to do blah.'); 
## or
 exit('Failed to do blah.'); 

Sorry Kalon, I called you professor oak.

Hi, brilliant I will do that in the future, a useful thing to know. How am I for mysql_real_ecape. I’m not sure exactly where I need to place them and if I need anymore than the one I have already?

i don’t understand why you’re doing this –


$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);

you’re returning the ~entire~ addresses table??

why?

as for checking to see if it exists before inserting it, you don’t really need to do that

just use INSERT with the ON DUPLICATE KEY UPDATE option if you want to capture the latest time for each email, or else use INSERT IGNORE to retain the first time for each email

Hi there, I’m returning the entire database table as my boss wants to receive a message each time someone subscribes that contains their email address and how many email addresses are on the database.

Thanks for your help, it’s appreciated. Have I used the mysql_real_escape correctly?

use COUNT(*)

please see http://www.sitepoint.com/forums/showpost.php?p=4487997&postcount=2

:slight_smile:

a better way is probably something like this

 
<?php
 
$query = 'select count(*) as "total" from someTable';
  
$row = mysql_fetch_assoc(mysql_query($query, $conn));
 
echo 'total number of rows = '.$row['total'];
 
?>

this way you are retrieving just 1 row from the database which contains a field with the total number of rows and not every row in the database.