tgavin Posted October 2, 2006 Share Posted October 2, 2006 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!).[code]<?phpfunction 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 }?>[/code] Quote Link to comment Share on other sites More sharing options...
effigy Posted October 2, 2006 Share Posted October 2, 2006 Turning your array into a hash (associative array) will automatically eliminate duplicates:[code]<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>[/code][quote]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)[/quote] Quote Link to comment Share on other sites More sharing options...
Barand Posted October 2, 2006 Share Posted October 2, 2006 in this line, the value for $email_check never gets set[code]if($email_check > 0) {[/code]Variables are [b]local[/b] to functions so the $email_check vatiable inside the function is not that used outside.You need[code] $email_check = email_check($email); if($email_check > 0) {[/code] Quote Link to comment Share on other sites More sharing options...
printf Posted October 2, 2006 Share Posted October 2, 2006 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...[code] email_check($email); if($email_check > 0) {[/code]should be[code] $check = email_check($email); if($check > 0) {[/code]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...[code]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;}[/code]Should be changed to[code]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'];}[/code]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! Quote Link to comment Share on other sites More sharing options...
tgavin Posted October 3, 2006 Author Share Posted October 3, 2006 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[code]<?phpif(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) {AND$bad_email[] = $email;?>[/code]I don't know what to do about this because I have to unset() *something* don't I? Quote Link to comment Share on other sites More sharing options...
Barand Posted October 3, 2006 Share Posted October 3, 2006 Put[code]continue;[/code]after each of the unset() calls. Quote Link to comment Share on other sites More sharing options...
tgavin Posted October 3, 2006 Author Share Posted October 3, 2006 [quote author=Barand link=topic=110297.msg445781#msg445781 date=1159851008]Put[code]continue;[/code]after each of the unset() calls.[/quote]Awesome! Thanks! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.