I have made a simple form where users who have been subscribed and unsubscribe by inserting their email address.
In my database using PHPMyAdmin, my database to store the emails is 'Links', the table is 'email' and the fields are the 'id' and 'emailaddress'.
What I have tried is making a text input field, where the user ill insert his or her email address, to unsubscribe on the website. As a result the user's field for his or her email address will be delete in the database which is saving the emails for all users who have subscribed.
$email = $_POST['enter'];
@mysql_connect ('localhost', 'root', '') or die ('Error');
@mysql_select_db ('links') or die ('Error');
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "Not an email";
return false;
} else {
mysql_query("DELETE FROM email WHERE emailaddress ='$email'");
echo "deleted";
}
}
?>
When I test it,it is not working, as I see the email which was saved, is still in the database! Help!
Here are codes including what you suggested me, but the problem is the same! Is this because my emailaddress field in the table is UNIQUE KEY?
PHP Code:
<?php if ($_SERVER['REQUEST_METHOD'] == 'POST') { $email = $_POST['enter']; @mysql_connect ('localhost', 'root', '') or die ('Error'); @mysql_select_db ('links') or die ('Error'); if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { echo "Not an email"; return false; } else { mysql_query("DELETE FROM email WHERE emailaddress ='$email'") or die(mysql_error()); echo "deleted"; } } ?>
Error reporting on doesn't necessarily mean that its on for all errors.
It would be preferable that the key is unique. Otherwise you can potentially delete multiple records; both are valid for a design depending on purpose. It is possible that you are violating a key constraint not set to cascade which will cause it to reject the deletion.
It just tells me that the email has been deleted, but when I refresh the database in PHPMyAdmin, it is still there.. I mean it has not been deleted at all..
Okay, so or die(mysql_error()); produces no failure results. The query is therefore successful indicating a deficiency with the data involved.
Now you check how many were deleted:
PHP Code:
mysql_query("DELETE FROM email WHERE emailaddress ='$email'") or die(mysql_error());
printf("Deleted %d records." . PHP_EOL, mysql_affected_rows());
How many does it say were deleted? If its 0, there is no matching emailaddress to the given $email. Capture the query into a variable and print that out instead.
I have already done it. It is working now. It was a problem of a dot in the database. When performing the INSERT code, I included a dot in it thus thats is why the email was not deleting as it did not match. Thank you to all of you who have helped me!