'PHP delete function with mySQL: fail to delete user from database

For a PHP project, I am trying to delete a user and all of his actions / info from my database. It is working for every table (posted likes, comments, projects,...), but it fails to delete the user itself, as if it cannot get the id (primary key).

How to fix this?


The delete function in my User Class:

public static function deleteAccount(int $id) {
    $conn = DB::getConnection();
    $statement = $conn->prepare("
    delete from social_links where user_id = :id ; 
    delete from following where user_follower = :id OR user_following = :id ;
    delete from likes where user_id = :id ;
    delete from comments where user_id = :id ;
    delete from projects where user_id = :id ; 
    delete from users where id = :id"
);
    $statement->bindValue(":id", $id);
    $statement->execute();
}

The checks and execution of this function:

if(!empty($_POST["userPW"]) && isset($_POST["delete-def"])) {
    $conn = DB::getConnection();
    $statement = $conn->prepare("select * from users where id = :id");
    $statement->bindValue(":id", $_SESSION["user"]["id"]);
    $statement->execute();
    $user = $statement->fetch(PDO::FETCH_ASSOC);
    $hash = $user["password"];

    if(password_verify($_POST["userPW"], $hash)) {
        User::deleteAccount($_SESSION["user"]["id"]);
        session_destroy();
        session_reset();
        header("Location: login.php");    
    } else {
        $error = "Password is incorrect.";
    }
}

The two-step interface: enter image description here

enter image description here



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source