<select>和$ _POST </select>

时间:2013-11-16 11:49:26

标签: php html mysql

我想编辑一个具有用户名,密码和权限的管理员。最后一个是这样的:

<?php $showQuery = mysqli_query($db, "SELECT * FROM admins WHERE id = {$id}"); 
$show = mysqli_fetch_assoc($showQuery); ?>

<p>Power:&nbsp; <select name = "power">
<option value <?php if ($show['power']=='superadmin') {echo " selected";} ?> = "superadmin">Superadmin</option>
<option value <?php if ($show['power']=='admin') {echo " selected";} ?> = "admin">Admin</option>
<option value <?php if ($show['power']=='moderator') {echo " selected";} ?> = "moderator">Moderator</option>
</select><br /><br />

然后我想抓住这些值:

<?php if (isset($_POST['editAdmin'])) {

$username = $_POST['username']; 
if (!$_POST['password'] == "") { $password = $_POST['password']; }
$power = $_POST['power'];

$editQuery = "UPDATE admins SET ";
$editQuery .= "username = '{$username}', ";
if (isset($password)) { $editQuery .= "hashed_pwd = '{$password}', "; }
$editQuery .= "power = '{$power}' ";
$editQuery .= "WHERE id = {$id} LIMIT 1";

$editAdmin = mysqli_query($db, $editQuery);

现在,如果我不想更改管理员的权限(保持选项不变)并点击编辑,则电源变为空白。但是,如果我更改电源(升级或降级),则设置为OK。我做错了什么?

2 个答案:

答案 0 :(得分:1)

你错误地构建你的选择,你有options这样设置:

<option value <?php if ($show['power']=='admin') {echo " selected";} ?> = "admin">Admin</option>

他们应该是这样的:

<option value="admin" <?php if ($show['power']=='admin') {echo " selected";} ?> >Admin</option>

答案 1 :(得分:1)

将选项输出更改为

<option value="superadmin" <?php if ($show['power']=='superadmin') {echo "selected";} ?>>Superadmin</option>