我如何让这个按钮与数据库交互

基本上我正在制作一个网站,上面会有管理员,但我想要它,这样管理员就可以添加其他管理员,所以我知道当管理员登录时它会显示网站上每个人的用户名,但我想要用户名旁边的按钮可更改其在数据库中的管理员角色,当前管理员是 true 或 false,我项目的脚本如下。希望有人可以帮助我,因为我似乎无法改变我所拥有的最多的用户是改变列表中的最后一个用户,但我已经删除了它,因为它不是我想要的。


<?PHP

include("auth.php");

require_once "config.php";


$result = mysqli_query($conn, "SELECT * FROM accounts");


?>


<!DOCTYPE HTML>

<html>

<head>

<meta charset="UTF-8">

<title>Welcome</title>

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">

<style type="text/css">

    body{ font: 14px sans-serif; text-align: center; }

</style>



</head>

 <body>

<div class="page-header">

    <h1>Hi, <b><?php echo htmlspecialchars($_SESSION["username"]); ?></b>. Welcome to our site.</h1>

</div>

<p>

    <a href="ResetPass.php" class="btn btn-warning">Reset Your Password</a>

    <a href="logout.php" class="btn btn-danger">Sign Out of Your Account</a>

</p>






<?php 





if($_SESSION["IsAdmin"] == 1)

{


    while ($row = mysqli_fetch_array($result)){

?>




<table id=Username_List>


<b><?php

        echo $row['username'];


    ?></b>

<a class="btn btn-danger">Toggle Admin</a>


</table>


<?php

}}

else{

?>


<span>Hello Customer</span>


<?php

}


?>

</body>

</html>


眼眸繁星
浏览 102回答 2
2回答

侃侃无极

要在数据库中切换用户类型,您需要获取用户 ID 和用户的当前角色。然后您必须将这些信息发送到 php 脚本以切换数据库中的用户角色。假设我们有一个名为 toggleUser.php 的 php 文件,它使用 get 方法接收用户 ID 和当前角色,那么你的 url 将如下所示:<a class="btn btn-danger" href="toggleUser.php?userid=<?php echo $row['userid']?>&role=<?php echo $row['role']?>">Toggle Admin</a>创建文件 toggleUser.php 并添加以下代码:<?php&nbsp; &nbsp; $userid = $_GET['userid'];&nbsp; &nbsp; $role&nbsp; &nbsp;= $_GET['role'];&nbsp; &nbsp; $role = ($role==1)?0:1;&nbsp; &nbsp; &nbsp; &nbsp; $stmt = mysqli_prepare($conn, "update accounts set&nbsp; IsAdmin=? where username=?");&nbsp; &nbsp; &nbsp; &nbsp; mysqli_stmt_bind_param($stmt, 'ss', $role,$userid);&nbsp; &nbsp; &nbsp; &nbsp; mysqli_stmt_execute($stmt);&nbsp; &nbsp; header("location:showusers.php");?>&nbsp; &nbsp;

繁花不似锦

您实际上也可以使用 PHP 解决问题。我假设“用户名”字段是唯一的。我通过将“Toggle Admin”放入由同一文件处理的表单中修改了您的代码的某些部分。单击按钮后,将执行添加到 PHP 块的代码。该代码从表单接收用户名和当前角色,然后相应地切换数据库中的“IsAdmin”。这是我的解决方案:<b><?php&nbsp; &nbsp; &nbsp; &nbsp; echo $row['username'];&nbsp; &nbsp; ?></b>&nbsp; &nbsp; <form action="" method="post">&nbsp; &nbsp; &nbsp; &nbsp; <input type="hidden" name="username" value="{$row['username']}">&nbsp; &nbsp; &nbsp; &nbsp; <input type="hidden" name="role" value="{$row['IsAdmin']}">&nbsp; &nbsp; &nbsp; &nbsp; <input type="submit" name="toggle_admin" class="btn btn-danger" value="Toggle Admin">&nbsp; &nbsp; </form>我还按如下方式更新了您的 PHP 块:<?PHPinclude("auth.php");require_once "config.php";$result = mysqli_query($conn, "SELECT * FROM accounts");// Execute this code if the 'Toggle Admin' button is clickedif (isset($_POST['toggle_admin'])){&nbsp; &nbsp; if (isset($_POST['username']) && !empty($_POST['username']) && isset($_POST['role']) && !empty($_POST['role']))&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // Retrieve necessary inputs&nbsp; &nbsp; &nbsp; &nbsp; $username = htmlentities($_POST['username']);&nbsp; &nbsp; &nbsp; &nbsp; $role = htmlentities($_POST['role']);&nbsp; &nbsp; &nbsp; &nbsp; // Toggle Admin&nbsp; &nbsp; &nbsp; &nbsp; if ($role == 0)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Set user to be admin if not currently admin&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $IsAdmin = 1;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Set user to be non admin if user is currently admin&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $IsAdmin = 0;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // Update the database accordingly.&nbsp; &nbsp; &nbsp; &nbsp; $perform_toggle_admin = mysqli_query($conn, "UPDATE accounts SET IsAdmin = '$IsAdmin' WHERE username = '$username'");&nbsp; &nbsp; }}?>希望这是有帮助的。
打开App,查看更多内容
随时随地看视频慕课网APP