使用php和sql更新用户状态

kokeuurv  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(371)

在这里,我试图获得一个管理用户列表,并使用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>
fkaflof6

fkaflof61#

要补充的是,

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

要删除,

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

yrefmtwq2#

语法:

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>

相关问题