表单未发布以删除SQL对象

时间:2019-03-28 18:42:03

标签: php

我有一个页面来管理用户,它可以很好地检索用户,但是当我尝试删除用户时,没有任何反应。

我正在使用apache2在php 7.2上运行。我检查了日志,没有发现任何中断或给出警告/信息级别。我尝试过切换查询和变量以及位置,但是它似乎没有生效。

<!DOCTYPE html>
<html lang="en">
<?php
$con=mysqli_connect("localhost:3306","user","pass","database");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }


$query = "SELECT `id`,`username`,`email`,`created_at`,`is_admin` FROM users ";


$result = mysqli_query($con,$query);


//if ($_SERVER["REQUEST_METHOD"] == "POST") {
if(isset($_POST['submit'])){
  if (empty($_POST["id"])) {
    $error = "Make a Selection";
  } else {
    $delete_id = test_input($_POST["id"]);
    $sql = mysqli_query($con,"DELETE FROM users WHERE id='$delete_id'");
    echo "<p>" . var_dump($sql) . "</p>";
    if($sql){
        echo "Deleted";
    }
  }
}

?>
<head>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
    <style type="text/css">
        body{
        font: 14px sans-serif;
         }
        .wrapper{
         width: 350px;
         vertical-align: middle;
         padding: 20px;
         }
</style>
</head>
<body>
<form method="post">
<table border='1'><tr><th></th><th>username</th><th>email</th><th>date joined</th><th>Permissions</th></tr>
<?php
while($row    = mysqli_fetch_assoc($result))
  {
  $id = $row['id'];
    if ($row['is_admin'] == 1) {
    $permissions ="Admin";
  }
  else {
    $permissions = "User";
 } 
  echo "<tr>";
  echo '<td><input type="checkbox"  name="ID" value="' . $id . '"></td>';
  echo "<td>" . $row['username'] . "</td>";
  echo "<td>" . $row['email'] . "</td>";
  echo "<td>" . $row['created_at'] . "</td>";
  echo "<td>" . $permissions . "</td>";
  echo "</tr>";
  }


echo "</table>";
echo "<BR>";
echo "<BR>";
echo "<BR>";
echo '<input type="submit" name="submit" class="btn btn-primary" value="Delete"> &nbsp  &nbsp  &nbsp  &nbsp';
echo '<input type="submit" class="btn btn-primary" value="Reset PW"><BR/><BR />';
echo "</form>";
mysqli_close($con);
?>
</body>
</html>

我希望选择的用户被删除,但删除不会发生

1 个答案:

答案 0 :(得分:0)

尝试以下操作:

replace:
if ($_SERVER["REQUEST_METHOD"] == "POST") {
with:
if(isset($_POST['submit'])){

并将name="submit"添加到您的提交按钮