PDO PHP: How to make a logged in user delete your account?

1

User accesses the logged in page:

<?php
session_start();
echo 'Bem vindo, '.$_SESSION['username'];
?>
<br /><a href='logout.php'>Logout</a>

I want him to push a button or link and delete his own account (without typing anything).

<?php
if(isset($_POST["button"])){
$hostname='localhost';
$username='root';
$password='';

try {
$dbh = new PDO("mysql:host=$hostname;dbname=projeto",$username,$password);

$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this     line
$del = "DELETE FROM tbl_users WHERE id =".$_SESSION['id'];


if ($dbh->query($sql)) {
echo "<script type= 'text/javascript'>alert('Conta deletada com sucesso');  </script>";
}
else{
echo "<script type= 'text/javascript'>alert('Falha');</script>";
}

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

}
?>

How do I do it?

    
asked by anonymous 21.08.2016 / 22:05

1 answer

0
            <?php
            session_start();

            if(isset($_POST["button"])){
            $hostname='localhost';
            $username='root';
            $password='';

            $user = $_SESSION['id'];

            try {
            $dbh = new PDO("mysql:host=$hostname;dbname=projeto",$username,$password);

            $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this     line

            $del = "DELETE FROM tbl_users WHERE id = :id";
            $deletar = $dbh->prepare($del);
            $deletar->bindValue(':id', $user);
            $retorno = $deletar->execute();


            if ($dbh->query($sql)) {
            echo "<script type= 'text/javascript'>alert('Conta deletada com sucesso');  </script>";
            }
            else{
            echo "<script type= 'text/javascript'>alert('Falha');</script>";
            }

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

            }
            ?>
    
22.08.2016 / 00:25