SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Evangelist
    Join Date
    Aug 2010
    Posts
    503
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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 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. #2
    Non-Member Kalon's Avatar
    Join Date
    Aug 2010
    Location
    At my computer
    Posts
    2,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this could be a good opportunity to enhance your debugging skills with some basic debugging

  3. #3
    SitePoint Evangelist
    Join Date
    Aug 2010
    Posts
    503
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  4. #4
    SitePoint Evangelist
    Join Date
    Aug 2010
    Posts
    503
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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!

  5. #5
    SitePoint Zealot Zurev's Avatar
    Join Date
    Feb 2009
    Posts
    171
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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:

    PHP Code:
     die();
    ## or
    exit(); 
    Do

    PHP Code:
     die('Failed to do blah.'); 
    ## or
     
    exit('Failed to do blah.'); 
    Sorry Kalon, I called you professor oak.

  6. #6
    SitePoint Evangelist
    Join Date
    Aug 2010
    Posts
    503
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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?

  7. #7
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,014
    Mentioned
    53 Post(s)
    Tagged
    2 Thread(s)
    i don't understand why you're doing this --
    PHP Code:
    $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
    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  8. #8
    SitePoint Evangelist
    Join Date
    Aug 2010
    Posts
    503
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by r937 View Post
    i don't understand why you're doing this --
    PHP Code:
    $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?

  9. #9
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,014
    Mentioned
    53 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by coxdabd View Post
    ... and how many email addresses are on the database.
    use COUNT(*)

    please see http://www.sitepoint.com/forums/show...97&postcount=2

    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  10. #10
    Non-Member Kalon's Avatar
    Join Date
    Aug 2010
    Location
    At my computer
    Posts
    2,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by coxdabd View Post
    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.
    a better way is probably something like this

    Code:
     
    <?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.


Bookmarks

Posting Permissions

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