如何添加post方法

noj0wjuj  于 2021-09-29  发布在  Java
关注(0)|答案(1)|浏览(448)

编辑:
单击此处查看图片(它在我的网站上的外观)
代码:

<form action="" method="post">
                  <input type = "submit" name="sql_update" value="SQL Update">
                  <?php $admin_checkbox = '<input type="checkbox" name="admin_status">';?>
                      </form>

$getusers = "SELECT id, username, firstname, lastname, admin FROM registered_users";
$result = mysqli_query($db, $getusers);

if ($result->num_rows > 0) {

    while($row = $result->fetch_assoc()) {
        echo "<br>". $row["id"]. " &nbsp; - &nbsp;  " . $row["username"]. "  &nbsp; - &nbsp;  " . $row["firstname"]. " &nbsp; - &nbsp;  " .$row["lastname"]. "  &nbsp; - &nbsp;  " .$row["admin"]. "  &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;  " .$admin_checkbox. "<br>";
    }
}

if (isset($_POST['sql_update'])) {
  if (empty($_POST["admin_status"])){
    echo ("Ni obkljukano");
  }

如何连接$admin_复选框以确保此代码正常工作。

if (empty($_POST["admin_status"])){
    echo ("Ni obkljukano");
  }

更新:
一切都很好!
现在我还想知道我如何能做一个反向效果。如果用户已经是管理员。我必须在elseif语句中键入什么。我真的很抱歉问了这么多问题,包括:/

if (isset($_POST['sql_update'])) {
  if (empty($_POST["admin_status"])){
    echo ("Ni izbranih elementov.");
  } 
  elseif(){

    $sql_admin_off = mysqli_query($db, "UPDATE registered_users SET admin = 0 WHERE id IN (".implode(',', $_POST['admin_status']).") ");
    $sql_admin_off;
    echo("test");

  }
  else {

    $sql_admin_on = mysqli_query($db, "UPDATE registered_users SET admin = 1 WHERE id IN (".implode(',', $_POST['admin_status']).") ");
    $sql_admin_on;
    echo("SQL uspešno zapisan!");

  }

    }

更新#2

if (isset($_POST['sql_update'])) {
  if (empty($_POST["admin_status"])){
    echo ("Ni izbranih elementov.");
  } else {

    $sql_admin_on = mysqli_query($db, "UPDATE registered_users SET admin = 1 WHERE id IN (".implode(',', $_POST['admin_status']).") ");
    $sql_admin_on;
    echo("SQL uspešno zapisan!");

    $sql_admin_off = mysqli_query($db, "UPDATE registered_users SET admin = 0 WHERE id NOT IN (".implode(',', $_POST['admin_status']).") ");
    $sql_admin_off;
    echo("test");
  }

  }
qvsjd97n

qvsjd97n1#

假设您已成功列出所有带有管理员复选框的用户:
HTML表单
当您列出所有用户并为每个用户设置复选框时,请将复选框值设置为 userId . 还可以使用字段名数组对所有复选框进行分组 name="adminId[]" . 后来 $_POST['adminId'] 将包含 ID's 其中选中了复选框。
然后,您可以简单地使用sql in 操作员更新“admin_rights=true”,如果中列出了id $_POST['adminId'] . 这个 Implode() 将用字符串连接数组元素( , ).

<form action="..." method="post">
    <label>
       User ID1: 
       <!-- Admin Checkbox -->
       <input type="checkbox" name="adminId[]" value="id1" />
    </label><br>
    <label>
       User ID2: 
       <!-- Admin Checkbox -->
       <input type="checkbox" name="adminId[]" value="id2" />
    </label><br>
    <label>
       User ID3: 
       <!-- Admin Checkbox -->
       <input type="checkbox" name="adminId[]" value="id3" />
    </label><br>
    <input type="submit" id="edit" name="edit"/>
</form>

php

<?php
// If edit button pressed
if( isset($_POST['edit'])) )
{
    // If Admin checkbox were selected then add Admin Rights
    if (isset($_POST['adminId']){
       $sql = 'UPDATE `usertable` SET `admin_rights`= 'true' WHERE id IN ('.implode(',', $_POST['adminId']).')';
       mysqli_query($sql);
    }
}
?>

似乎我误解了您的问题,您仍然无法使用复选框(作为一个整体表单)列出用户。
请看下表:

<form action="" method="post">
    <input type = "submit" name="sql_update" value="SQL Update">

<?php
   $getusers = "SELECT id, username, firstname, lastname, admin FROM registered_users";
   $result = mysqli_query($db, $getusers);

   if ($result->num_rows > 0) {
      while($row = $result->fetch_assoc()) {
         if ($row['admin'] == 1){
              echo "<br>". $row['id']. " &nbsp; - &nbsp;  " . $row['username']. "  &nbsp; - &nbsp;  " . $row['firstname']. " &nbsp; - &nbsp;  " .$row['lastname']. "  &nbsp; - &nbsp;  " .$row['admin']. "  &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; <input type='checkbox' name='admin_status[]' value='".$row['id']."' checked><br>";
         }
         else echo "<br>". $row['id']. " &nbsp; - &nbsp;  " . $row['username']. "  &nbsp; - &nbsp;  " . $row['firstname']. " &nbsp; - &nbsp;  " .$row['lastname']. "  &nbsp; - &nbsp;  " .$row['admin']. "  &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; <input type='checkbox' name='admin_status[]' value='".$row['id']."'><br>";
       }
   }
?>
</form>

要检查的代码 $_POST 价值:

<?php
   if (isset($_POST['sql_update'])) {
       if (empty($_POST["admin_status"])){
         echo ("Ni obkljukano");
       }
   }
?>

更新的进一步更新#2

if (isset($_POST['sql_update'])) {
  // All unchecked, set all admin to 0
    if (empty($_POST['admin_status'])){
        $sql_admin_off = mysqli_query($conn, "UPDATE user SET admin = 0");
    }
    else{
        $sql_admin_on = mysqli_query($conn, "UPDATE user SET admin = 1 WHERE id IN (".implode(',', $_POST['admin_status']).") ");
        $sql_admin_off = mysqli_query($conn, "UPDATE user SET admin = 0 WHERE id NOT IN (".implode(',', $_POST['admin_status']).") ");
    }
}

相关问题