PHP - How to remove a user from the database MySQL

2.3k Views Asked by At

I created a table where users are discharged from the database. Each user has a row and next to it is a button for deleting a user from the database. The problem is that I do not know how to do to really removed the particular user from the database when I click on a specific button. I have no idea how to do it.

<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"pl-PL\">
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <title>Rezultat zapytania</title>
    <style>
    #container {
      max-width: 1000px;
    }
    </style>
</head>

<body>

<div id="container">
    <table width="1000" align="center" border="1" bordercolor="#d5d5d5"  cellpadding="0" cellspacing="0">
        <tr>
        <?php
        require_once "connect.php";
        $baza = @new mysqli($host, $db_user, $db_password, $db_name); //Połączenie z bazą danych
        $sql = "SELECT id, user, pass, email FROM users";
        $result = $baza->query($sql);
        $ile = mysqli_num_rows($result);

        echo "znaleziono: ".$ile."<br /><br />";
if ($ile>=1)
{
echo<<<END
<td width="50" align="center" bgcolor="e5e5e5"></td>
<td width="50" align="center" bgcolor="e5e5e5">IDUsera</td>
<td width="20" align="center" bgcolor="e5e5e5">NazwaUsera</td>
<td width="20" align="center" bgcolor="e5e5e5">HasloUsera</td>
<td width="20" align="center" bgcolor="e5e5e5">EmailUsera</td>
</tr><tr>
END;
}

    for ($i = 1; $i <= $ile; $i++)
    {

        $row = mysqli_fetch_assoc($result);
        $a1 = $row['id'];
        $a2 = $row['user'];
        $a3 = $row['pass'];
        $a4 = $row['email'];

echo<<<END
<td width="50" align="center"><a class="button_red" href="delete_user.php">Delete</a></td>
<td width="50" align="center">$a1</td>
<td width="100" align="center">$a2</td>
<td width="100" align="center">$a3</td>
<td width="100" align="center">$a4</td>
</tr><tr>
END;

    }


?>


</tr></table>
</div>


</body>
</html>
1

There are 1 best solutions below

2
On
// create a <td> tag in <tr>
 <td><a href="del.php?user_id={$id}">DEL USER</a></td>

/* del.php */

$user_id = $_GET['user_id'];
// you can add a sql injection filter
$sql = 'DELETE FROM table WHERE user_id = {$user_id}';
$result = $baza->query($sql);