Delete script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DavidPr
    New Member
    • Mar 2007
    • 155

    Delete script

    I'm using the delete script below. It's deleting and sending the email OK, but the correct information is not being sent.

    I tested this script on one database and it deleted several ads, but the email I received said that "No ads were deleted." How do I fix this?

    delete_old.php:
    Code:
    <?php
    include("dbconnect.php");
    $query = "DELETE FROM ads WHERE submitted < SUBDATE(NOW(), INTERVAL 45 DAY)";
    $result = mysql_query($query);
    
    if (mysql_affected_rows() == 1)
    { // A record was deleted.
    $action = $result;
    } else {
    $action = "No ads were deleted.";
    }
    
    // Send the email.
    $when = date('j M Y h:i:s A');
    
    $email = "here@there.com";
    $subject = "Ad Deletion";
    $body = "Date: <b>$when</b><br><br> Results: $action";
    $headers = "From: there@here.com\n";
    $headers .= "Content-Type: text/html; charset=iso-8859-1\n";
    mail ($email, $subject, $body, $headers);
    ?>
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    It sounds like you're not being very specific with your DELETE clause. What I mean is, it looks like multiple records could be getting deleted. This would mean that mysql_affected_ rows() may not necessarily be 1, but greater than one. So, I suggest you edit your IF statement to check for a value of greater than 0 (hint: > 0).

    Hope this helps,
    Markus.

    Comment

    Working...