使用PHP和SQL更新用户状态

时间:2018-09-02 05:15:33

标签: php mysql sql crud

在这里,我正在尝试获取管理员用户列表,并使用PHP和SQL对其进行添加和删除。

我列出了一些用户,但我不想通过单独更新布尔数据(使该布尔数据声明其有效或无效)来进行删除和添加。

非常感谢任何人的帮助。

谢谢。

<?php
// connection to database
require 'config.php';

    $sql = "SELECT id, adminUser, password FROM admins WHERE legal = '1'";
    $result = mysqli_query($conn, $sql);


    $sql2 = "SELECT id, adminUser, password FROM admins WHERE legal = '0'";
    $result2 = mysqli_query($conn, $sql2);   

?>


<!DOCTYPE html>
<html>

<body class="w3-black">    

<!-- Page Content -->
<div class="w3-padding-large" id="main">
  
<div class="w3-padding-64 w3-content w3-text-grey" id="contact">
  <table>
    <tr>
      <th>ID</th>
      <th>Admin names</th>
      <th>Status</th>
      <th>Action</th>
    </tr>

    <?php
      if (mysqli_num_rows($result) > 0) {
          // output data of each row
          while($row = mysqli_fetch_assoc($result)) {
            
              echo "<tr>
                      <td>". $row["id"]. "</td>
                      <td>". $row["adminUser"]. "</td>
                      <td>valid</td>
                      <td><a href='remove.php'>remove</a></td>
                    </tr>" ;
          }
      }  
    ?>

    <?php
      if (mysqli_num_rows($result2) > 0) {
          // output data of each row
          while($row2 = mysqli_fetch_assoc($result2)) {
            
              echo "<tr>
                      <td>". $row2["id"]. "</td>
                      <td>". $row2["adminUser"]. "</td>
                      <td>invalid</td>
                      <td><a href='add.php'>add</a></td>
                    </tr>" ;
          }
      }  
    ?>
  </table>
</div>

<div class="w3-padding-64 w3-content w3-text-grey" id="contact"> 
  <a href="adminpage.php">Go Back</a>
</div>

  
<!-- END PAGE CONTENT -->
</div>


</body>
</html>

2 个答案:

答案 0 :(得分:0)

要添加

"UPDATE admins SET legal = '1' WHERE id = '<target admin id>'";

要删除,

"UPDATE admins SET legal = '0' WHERE id = '<target admin id>'";

答案 1 :(得分:0)

语法:

UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value

要使用PHP更新(update.php):

$sql = "";
$action = mysqli_real_escape_string($con, $_GET['action']);
$id = mysqli_real_escape_string($con, $_GET['id']);
if($action == "add") {
    $sql = "UPDATE admins SET legal='1' WHERE id='".$id."'";
} else {
    $sql = "UPDATE admins SET legal='0' WHERE id='".$id."'";
}

if (mysqli_query($con, $sql)) {
    echo "Record updated successfully";
} else {
    echo "Error updating record: " . mysqli_error($con);
}

,您应该更改锚标记

<td><a href='update.php?action=remove&id=<?= $row['id'] ?>'>remove</a></td>

<td><a href='update.php?action=add&id=<?= $row2['id'] ?>'>add</a></td>