我正在尝试在我的用户列表页面上创建一个按钮来删除该行,或者让该用户成为管理员。
以下是用户查询和html的信息:
<?php
$query = "SELECT * FROM users";
try
{
$stmt = $db->prepare($query);
$result = $stmt->execute();
}
catch(PDOException $ex)
{
die("An Error has occured. Please contact the server administrator for assistance.");
}
$rows = $stmt->fetchAll();
?>
<?php foreach($rows as $row) : ?>
<?php
if($row['usertype'] == 2) {
$usertype = "<span style='color:#F7FE2E;'>Donator</span>";
} elseif($row['usertype'] == 3) {
$usertype = "<span style='color:red;'>Admin</span>";
} elseif($row['usertype'] == 4) {
$usertype = "<span style='color:orange;'>Owner</span>";
} else {
$usertype = "<span style='color:#585858;'>Normal</span>";
}
?>
<tr>
<!--<td><?php echo $row['id']; ?></td>-->
<td><?php echo htmlentities($row['username'], ENT_QUOTES, 'UTF-8');?></td>
<!--<td><?php echo htmlentities($row['email'], ENT_QUOTES, 'UTF-8');?></td>-->
<td><?php echo htmlentities($row['steamid'], ENT_QUOTES, 'UTF-8');?></td>
<td><?php echo $usertype?></td>
<td><form action="" method="post">
<input type="submit" name="admin" value="Promote" />
</form></td>
</tr>
<?php endforeach; ?>
我准备并执行更新查询的代码:
if(!empty($_POST['admin']))
{
$query = "UPDATE `users` SET `usertype` = '3' WHERE `id` = " . $row['id'];
// $query_params = array(':id' => $row['id']);
try
{
$stmt = $db->prepare($query);
$result = $stmt->execute();
}
catch(PDOException $ex)
{
die("An Error has occured. Please contact the server administrator for assistance.");
}
}
不幸的是,当我运行当前设置时,它会更新最后一行。为了进一步询问我在寻找什么,我是否有一个用户列表:
在哪里&#34; admin_b&#34;是一个强制$_POST['admin']
admin_b
admin_b
admin_b
admin_b
更新:
因此,在我的表单中,我有<input type="hidden" name="id" value="<?php $row['id']; ?>" />
的输入,并将其添加到我的SQL $query = "UPDATE users SET usertype = '3' WHERE id = :id";
$query_params = array(':id' => $_POST['id']);
try
{
$stmt = $db->prepare($query);
$result = $stmt->execute();
}
catch(PDOException $ex)
{
die("An Error has occured. Please contact the server administrator for assistance.");
}
答案 0 :(得分:1)
发送带有$ _POST请求的ID,现在你总是更新用户id = $ row ['id']
WHERE `id` = " . $row['id'];
答案 1 :(得分:0)
row[id]edit?=edit.php= ...
并且让我们说你列出了所有成员,旁边是一个href,上面的代码将执行,它会显示让我们说比利?edit.php = 1,其中1是他的主键,然后为下一个,当你将光标滚动到下一个用户Jim的下一个href时,它会在你的edit.php中显示Jim?edit.php = 2,
if(isset($_POST['edit])){
code goes here to make the user an admin..
你也可以为删除创建一个href,类似于这个编辑.. 这只是我可以给你的一个想法/提示,但你的问题可以通过许多不同的方式解决,这取决于你如何做到这一点的方法:D goodluck。