使用sql编辑数据库中的用户

时间:2018-06-03 23:30:25

标签: php html sql database twitter-bootstrap

我尝试生成一个包含所有用户帐户的表,然后在每个用户旁边放置按钮,以删除或更改用户的帐户级别(admin或不是admin)。什么是最好的方法呢?

这是我的代码:

<?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>'; ?>

任何帮助将不胜感激:)

编辑:admin / standard用户通过user_level设置,0为标准用户,1为admin

编辑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语句以获得有关数据库行更改的反馈

2 个答案:

答案 0 :(得分:0)

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

<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>

答案 1 :(得分:-1)

如果没有完整的细节,就会出现这样的情况:

 <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.");