【问题标题】:Edit users in database using sql使用 sql 编辑数据库中的用户
【发布时间】:2018-06-03 23:30:24
【问题描述】:

我正在尝试生成所有用户帐户的表格,然后在每个用户旁边都有按钮来删除或更改用户的帐户级别(管理员或非管理员)。解决此问题的最佳方法是什么?

这是我的代码:

<?php
$query = mysql_query("SELECT  user_name,user_id,user_email,user_level
                    FROM users
                    ORDER BY user_name ASC");

echo '<table class="table table-hover">
      <thead class="thead-default">
      <tr>
        <th>Username</th>
        <th>User ID</th>
        <th>Email</th>
        <th>Level</th>
        <th>Options</th>
       </tr>
       </thead>';

while($row = mysql_fetch_array($query)){

  echo '<tbody>
         <tr>
           <td class="col-3">' .$row['user_name'].'</td>
           <td class="col-3">' .$row['user_id'].'</td>
           <td class="col-3">' .$row['user_email'].'</td>
           <td class="col-3">' .$row['user_level'].'</td>  
           <td class="col-3"><a class="btn btn-success" href="#" data-toggle="tooltip" title="Edit"><span class="glyphicon glyphicon-pencil"></span></a>
            <a class="btn btn-success" href="#" data-toggle="tooltip" title="Promote"><span class="glyphicon glyphicon-arrow-up"></span></a>
            <a class="btn btn-danger" href="#"><span class="glyphicon glyphicon-arrow-down" data-toggle="tooltip" title="Demote"></span></a>
            <a class="btn btn-danger" href="delete.php" data-toggle="tooltip" title="Delete"><span class="glyphicon glyphicon-trash"></span></a>
           </td> 
          </tr>
          </tbody>';
       }
   echo '</table>'; ?>

任何帮助将不胜感激:)

编辑:管理员/标准用户通过 user_level 设置,0 为标准用户,1 为管理员

编辑 2:添加代码

    <?php
include 'connect.php';
include 'header.php';

mysql_query("UPDATE users SET user_level='1' WHERE user_id='".$_GET['user_id']."'");
die("User promoted to admin.");

include 'footer.php';
?>

运气不好,将尝试添加 if 语句以反馈数据库行是否更改

【问题讨论】:

  • 人们在不知道数据库结构是什么样子的情况下无法帮助您解决这个问题。例如,users.user_level 的可能值是多少?
  • user_level 的值为 1 和 0。其中 1 是管理员,0 是标准用户。尝试使用您在下面发送的代码并将其编辑为我拥有的结构,但仍然没有运气
  • mysql_* 已弃用,不应在新代码中使用,并且您很容易受到 SQL 注入攻击。
  • @Johnny 您确定您正确连接到数据库吗?你遇到了什么错误?
  • 没有错误消息,正确连接到数据库,因为我在整个站点中使用相同的连接页面

标签: php html sql database twitter-bootstrap


【解决方案1】:

这是代码示例,因为您的问题缺少一些细节,所以我分享我的代码。

<table>
<tr>
<th>User Email </th>
<th>Date & Time </th>
<th>Complain Number</th>
<th>Complain Type</th>
<th>Description</th>
<th>Status</th>

</tr>
<?php 
$ccount =1;

$email= $_SESSION["email"];
$query = mysqli_query($con,"Select * from new_complain where new_email = 
'$email'");
while($rows = $query->fetch_assoc())
{
 ?>
<tr>

<input type="hidden" name="<?php echo 'sstd' . $ccount ; ?>" value="<?php 
echo $rows['complain_type']; ?>" placeholder="Student Name" />
<td><?php echo $_SESSION["email"]; ?></td>
<td><?php echo $rows['complain_date']; ?></td>
<td><?php echo $rows['new_id']; ?></td>
<td><?php echo $rows['complain_type']; ?></td>
<td><?php echo $rows['new_complain']; ?></td>
<td><?php echo $rows['comlain_status']; ?></td>

 </tr>

<?php 
  $ccount++;
   } ?>
</table>

【讨论】:

    【解决方案2】:

    如果没有完整的细节,它会是这样的:

     <a class="btn btn-success" href="promote.php?id='.$row['user_id'].'" data-toggle="tooltip" title="Promote"><span class="glyphicon glyphicon-arrow-up"></span></a>
     <a class="btn btn-danger" href="demote.php?id='.$row['user_id'].'"><span class="glyphicon glyphicon-arrow-down" data-toggle="tooltip" title="Demote"></span></a>
    

    那么你需要一个promote.php 和这个文件在同一个目录下,看起来像这样:

    <?php
    mysql_query("UPDATE users SET user_level='1' WHERE user_id='".$_GET['user_id']."'");
    die("User promoted to admin user.");
    

    还有一个像这样的demote.php

    <?php
    mysql_query("UPDATE users SET user_level='0' WHERE user_id='".$_GET['user_id']."'");
    die("User demoted to standard user.");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-19
      • 2013-04-08
      • 2011-10-10
      • 2013-06-18
      • 2022-11-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多