Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Check for Duplicates?

    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 Code:
    <?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\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);
        }
    }
    ?>

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    PHP Code:
        if ($numRow>0){
        exit();
        } 
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •