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...............

Recommended Answers

All 2 Replies

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.

Thanks very much, that did the trick!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.