Jump to content


Photo

checking for duplicates


  • Please log in to reply
6 replies to this topic

#1 tgavin

tgavin
  • Members
  • PipPipPip
  • Advanced Member
  • 176 posts
  • LocationNaples, FL

Posted 02 October 2006 - 10:25 PM

I have a textarea where an unlimited amount of email addresses can be added for insertion into a db. Each address is separated by a newline. I also have a checkbox, that when ticked will allow a check for duplicate addresses. This works for the most part, except that it spits out undefined index errors. Apparently I'm unsetting the wrong thing, or something. I'm not sure what it is, because if I try to insert 3 duplicate addresses, 1 will get inserted and 2 won't. The 2 dups are printed to the screen, but so are 2 undefined index errors for each address not inserted (4 total!).
<?php
function email_check($email) {
$query = "SELECT email FROM subscribers WHERE email='".$email."'";
	$sql = mysql_query($query,CONN) or die(sql_error('could not retrieve email from subscribers'));
	$email_check = mysql_num_rows($sql);
	return $email_check;
}
		
// set up vars
$dup_error = '';
$bad_error = '';
$newemails = trim($_POST['emails']);
$emails = preg_split('/[\r\n]+/', $newemails);

foreach($emails as $email) {
	// check for duplicate email addresses
	if(isset($_POST['check_dups'])) {
		email_check($email);
		if($email_check > 0) {
			// email address already exists!
			$dup_error = 1;
			$dup_email[] = $email;
			$dup_count = count($dup_email);
			unset($email);
		}
	}
	// check for bad email formatting / syntax.
	if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) {
		$bad_error = 1;
		$bad_email[] = $email;
		$bad_count = count($bad_email);
		unset($email);
	} else {
		// email passed inspection, insert it
		// blah, blah, blah
	}
?>

A noble spirit embiggens the smallest man.
- Jebediah Springfield

#2 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 02 October 2006 - 10:33 PM

Turning your array into a hash (associative array) will automatically eliminate duplicates:

<pre>
<?php

	$emails = array(
		'abc@123.com',
		'abc@123.net',
		'abc@123.net',
		'user@site.co.uk',
		'123@abc.org',
		'user@site.co.uk',
	);

	print_r($emails);
	$emails = array_flip($emails);
	print_r($emails);

?>
</pre>

Array
(
    [0] => abc@123.com
    [1] => abc@123.net
    [2] => abc@123.net
    [3] => user@site.co.uk
    [4] => 123@abc.org
    [5] => user@site.co.uk
)
Array
(
    [abc@123.com] => 0
    [abc@123.net] => 2
    [user@site.co.uk] => 5
    [123@abc.org] => 4
)


Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#3 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 02 October 2006 - 10:41 PM

in this line, the value for $email_check never gets set
if($email_check > 0) {

Variables are local to functions so the $email_check vatiable inside the function is not that used outside.

You need
		$email_check = email_check($email);
		if($email_check > 0) {

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#4 printf

printf
  • Staff Alumni
  • Advanced Member
  • 889 posts

Posted 02 October 2006 - 10:48 PM

options, options, they make things so difficult! You have (2) options, (1) load the emails into a email array, then check that array for a match, or (2) do what your doing, but fix some problems that you shouldn't be doing!

First your problems!

email_check($email);

A function must be assigned a $ouput variable, so that you have a return value. You don't assign any variable to hold the return value! so this...

email_check($email);
		if($email_check > 0) {

should be

$check = email_check($email);
		if($check > 0) {

Next, your query is selecting a value you don't need, in other words your selecting rows of data that your function doesn't need to return.

So this...

function email_check($email) {
$query = "SELECT email FROM subscribers WHERE email='".$email."'";
	$sql = mysql_query($query,CONN) or die(sql_error('could not retrieve email from subscribers'));
	$email_check = mysql_num_rows($sql);
	return $email_check;
}


Should be changed to

function email_check($email) {
$query = "SELECT COUNT(*) AS total FROM subscribers WHERE email='".$email."'";
	$sql = mysql_query($query,CONN) or die(sql_error('could not retrieve email from subscribers'));
	$result = mysql_fetch_assoc($sql);
	return $result['total'];
}

Also the email regex should be changed because it will disallow many valid emails, also use Perl compatibly regular expression as it's faster than POSIX, and is unicode safe!

me!

#5 tgavin

tgavin
  • Members
  • PipPipPip
  • Advanced Member
  • 176 posts
  • LocationNaples, FL

Posted 03 October 2006 - 12:18 AM

Effigy: Thank you. I tried your method as it seemed easy. Must have been too easy because it didn't work for me. Since I'm a noob with arrays I probably did it incorrectly.

Barand, printf: Thank you. I updated with your suggestions/corrections and am still having a problem. Apparently I'm unsetting the $email var because I'm getting 'undefined variable: email' errors on the following lines
<?php
if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) {

AND

$bad_email[] = $email;
?>
I don't know what to do about this because I have to unset() *something* don't I?
A noble spirit embiggens the smallest man.
- Jebediah Springfield

#6 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 03 October 2006 - 04:50 AM

Put
continue;

after each of the unset() calls.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#7 tgavin

tgavin
  • Members
  • PipPipPip
  • Advanced Member
  • 176 posts
  • LocationNaples, FL

Posted 03 October 2006 - 01:16 PM

Put

continue;
after each of the unset() calls.

Awesome! Thanks!
A noble spirit embiggens the smallest man.
- Jebediah Springfield




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users