Monk Monk - 1 month ago 5x
MySQL Question

How to delete selected user? PDO

I am using PDO and I want to remove specified user from my members list. I made "delete" button to show on every single user, and I want when I click on any random user, to delete him. This is my second day trying to make that.

I made something like this:

$id = $user->id;
$sql = "DELETE FROM `users` WHERE `id` = :id";
$query = $handler->prepare($sql);
$query->execute(array(":id" => $id));

But this is not a solution, this will delete everyone why join that page. I want to make when i click "delete" button, to remove the user where that button was. I hope you understand me, I'll show you the picture how I want it to be.

enter image description here


You have Either get the user id of the deleting using via $_REQUEST or $_POST methods.

I will figure it out where you have gone wrong.

The User ID retrival based on the HTML that you have placed.

// This user id can be obtained in Two ways as i suggested.
// 1.) $_REQUEST['user_id'] if you pass the data in the href link
// 2.) $_POST['user_id'] if you pass the data as hidden field in form element.
// 3.) Ensure that you get your USER ID correct  in that place by echoing the query that you have made in the SQL.
<a href="process.php?delete_id=<?php echo $data->id; ?>">DELETE</a>

// Ensure DB connectivity in this page. And this code will work fine.
    $id = $_REQUEST['delete_id'];
    $sql = "DELETE FROM `users` WHERE `id` ='".$id."'";
    $query = $handler->prepare($sql);
    $results = $query->execute();