Why does my Php Unsubscribe form return a 500 error?

37 Views Asked by At

I am trying to make a simple unsubscribe that lets user delete their email(or all) from mysql db on my website named "workovh7_messages_db".It returns 500 error "This page isn't working right now' I am able to add a name to the db just fine. I am hosted on Bluehost using mysql.

I expected the Delete to remove the email from the email field in the messages table. i would like to keep the name and phone but not necessary.There are only 4 fields, firstname, lastname, email, and phone. the bind value statement may be wrong but that the only way I can think to do it. Is there a way to do this or do I have to do the whole row? The user won't know the Id of their row. Is there a simple way to do this? do I have to match all the form data? Not sure what other methond to make an unsubscribe there are. This looks like it should do what I want, or would a replace the value be better.

<?php
Global $db;

// MySQL Settings:
$dbuser='xxxxxx'; // my admin account
$dbpassword='xxxxx';  // password for local master is on the list ""
$dbname='workovh7_messages_db'; // local db name
$dbconnection = "localhost";  
$db_source = 'mysql:host='.$dbconnection.'; 
                        dbname='.$dbname.';
                        dbUser='.$dbuser.';
                      dbPass='.$dbpassword;

try {
    $db = new PDO($db_source,
        $dbuser,
        $dbpassword
    );
    $db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
    $db->setAttribute( PDO::ATTR_EMULATE_PREPARES, TRUE );
} catch (PDOException $e) {
    $error_message = $e->getMessage();
    include('errors/db_error.php');
    exit;
}

$query = 'DELETE FROM messages
    WHERE email = :email';
    
try {
$statement = $db->prepare($query);
$statement->bindValue(':email', $mesages_db);           
$statement->execute();
$statement->closeCursor();

echo "Created.";

echo "<script>
         alert('unsubscribe sent succesfully'); 
         window.history.go(-1);
 </script>";

} catch (PDOException $e) {
echo $e->getMessage();

}
?>
0

There are 0 best solutions below