0

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();

}
?>
Rob-Ott
  • 11
  • 2
  • Hey, Rob! I'm Edward and I'm trying to help you. Can you explain more what are you trying to acchieve? For example `$query` and `$messages_db` is not defined in your code. Can you please edit your answer so I can know what exactly is being passed in `$db->prepare()`? – Eduardo Procópio Gomez Apr 30 '23 at 02:17
  • Also, if you could include the definition in your question, this could help other people to quickly test your code. – Eduardo Procópio Gomez Apr 30 '23 at 02:28
  • 2
    You can also enable debugging to see exactly what's going on with `ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);` and post it right here. – Eduardo Procópio Gomez Apr 30 '23 at 02:49
  • 1
    `alert()` and `window.history.go(-1)` is a really annoying and inelegant way to move to a page. Just kick them back to the "referer" if necessary using a `Location` header. – tadman Apr 30 '23 at 03:18
  • PSA: When you have 500 errors you have a server error and you need to **check your server error log** for details. – tadman Apr 30 '23 at 03:19
  • I changed the $sql to $query. I am not sure I need the binding I wanted to bind emails from the unsubscribe form in HTML. Do I put echo before the debugging code? – Rob-Ott Apr 30 '23 at 04:18
  • 1
    DELETE will remove the entire row. To delete one column you need UPDATE, using id column – Your Common Sense Apr 30 '23 at 04:46

0 Answers0