PHP - 如何从数据库MySQL中删除用户

时间:2016-12-31 15:41:25

标签: php mysql database delete-row

我创建了一个用户从数据库中退出的表。每个用户都有一行,旁边是一个用于从数据库中删除用户的按钮。问题是当我点击特定按钮时,我不知道如何从数据库中真正删除特定用户。我不知道该怎么做。

<!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 个答案:

答案 0 :(得分:0)

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