0

Can someone have a look at some code for me?

Here the problem. This is meant to delet an email address form a mysql table then alert the user this has been done. If the address does not exist then it alerts the user to that too.

But even if the address ddoes not exist it alerts the user that the address has been deleted!

Heres the code

if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
   if($r = mysql_query("DELETE FROM maillist WHERE email = '$mail'")){
      echo '<script type="text/javascript"> alert("Email Address Has Been Deleted") </script>';
   }
   else {
      echo '<script type="text/javascript"> alert("Email Does Not Exist In Database") </script>';
   }
   }
   else {
      echo '<script type="text/javascript"> alert("This Is Not A Valid Email Address!") </script>';
   }
   }

Thanks for looking...............

2
Contributors
2
Replies
9
Views
4 Years
Discussion Span
Last Post by GlenRogers
0

That is because a DELETE query will always return TRUE if it could be executed, and it can be executed, even if no rows are affected by it.

If you want to count the number of rows affected (the number of rows deleted), which would tell you if anything has actually been deleted, you can use the following:

if (filter_var($email, FILTER_VALIDATE_EMAIL)) 
{
    $r = mysql_query("DELETE FROM maillist WHERE email = '$mail'");

    if($r)
    {
        $affected_rows = mysql_affected_rows();

        if($affected_rows)
        {
            echo '<script type="text/javascript"> alert("Email Address Has Been Deleted") </script>';
        }
        else
        {
            echo '<script type="text/javascript"> alert("Email Does Not Exist In Database") </script>';
        }

    }
    else 
    {
        // Query could not be executed.
    }
}
else 
{
    echo '<script type="text/javascript"> alert("This Is Not A Valid Email Address!") </script>';
}

I used mysql_affected_rows() to count the number of rows affected by your query.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.