我有这段代码:
<?php $res = $conn->query("SELECT * FROM users");
//If the result of the query has more than 0 rows (1 or more) continue to the loop
if(mysqli_num_rows($res) >0){
//Loop to organise the data
while($row = mysqli_fetch_array($res)){
if($row['permission'] == "2"){
$perms = '<span class="label label-danger">Administrator</span>';
} elseif($row['permission'] == "1"){
$perms = '<span class="label label-success">Registered User</span>';
} else {
$perms = '<span class="label label-warning">Awaiting Approval</span>';
}
//Return the table data
echo "<tr><td>" . $row['firstname'] . "</td><td>" . $row['surname'] . "</td><td>" . $row['email'] . "</td><td>" . $row['username'] . "</td><td>" . $row['telephone'] . "</td><td>" . $perms . "</td>";
}
}?>
我想要的是在新列中为每一行添加一个按钮,该行将激活一段代码以删除数据库中的记录。
想知道是否有人可以协助或指出我如何做到这一点的正确方向。最终,将有一个删除按钮和一个批准按钮,它将激活位于同一文件中的不同SQL代码。 感谢
答案 0 :(得分:0)
在代码之上,如果通过帖子表单提供,则会发现删除此操作的操作...
它不是很安全,但我会添加一些安全检查,如:是号码?是有效的用户ID?什么是referal页面? ...
<?php
if ( $_POST["delete_id"] ) {
// This action will be performed after click on the Delete button.
// Better make some security checks here
$delete_id = htmlspecialchars($_POST["delete_id"]);
$res = $conn->query("DELETE FROM `users` WHERE `id` = '$delete_id' LIMIT 1;");
}
$res = $conn->query("SELECT * FROM users");
//If the result of the query has more than 0 rows (1 or more) continue to the loop
if(mysqli_num_rows($res) >0){
//Loop to organise the data
while($row = mysqli_fetch_array($res)){
if($row['permission'] == "2"){
$perms = '<span class="label label-danger">Administrator</span>';
} elseif($row['permission'] == "1"){
$perms = '<span class="label label-success">Registered User</span>';
} else {
$perms = '<span class="label label-warning">Awaiting Approval</span>';
}
// Creates a button with POST variable "delete_id" and the value of the "user id"
$button = '<form method="post" action="' . $_SERVER["PHP_SELF"] . '"><input type="hidden" name="delete_id" value="' . $row['id'] . '"><input type="submit" value="Delete this row"></form>';
//Return the table data
echo "<tr><td>" . $row['firstname'] . "</td><td>" . $row['surname'] . "</td><td>" . $row['email'] . "</td><td>" . $row['username'] . "</td><td>" . $row['telephone'] . "</td><td>" . $perms . "</td><td>" . $button . "</td>";
}
}
?>