我一直在努力让我的输入单选按钮在编辑帐户页面中设置预设值。我希望扮演'#角色'从我的数据库中的值查看编辑帐户页面时检查输入。
编辑:我想在编辑帐户页面上检查用户先前选择的单选按钮,因此请使用数据库中的值来选择应检查哪个输入。
// Initial query parameter values
$query_params = array(
':rank' => $_POST['rank'],
':role' => $_POST['role'],
':user_id' => $_SESSION['user']['id']
);
// If the user is changing their password, then we need parameter values
// for the new password hash and salt too.
if($password !== null)
{
$query_params[':password'] = $password;
$query_params[':salt'] = $salt;
}
// Note how this is only first half of the necessary update query. We will dynamically
// construct the rest of it depending on whether or not the user is changing
// their password.
$query = "UPDATE users SET rank = :rank";
$query .= ", role = :role";
// If the user is changing their password, then we extend the SQL query
// to include the password and salt columns and parameter tokens too.
if($password !== null)
{
$query .= "
, password = :password
, salt = :salt
";
}
// Finally we finish the update query by specifying that we only wish
// to update the one record with for the current user.
$query .= "
WHERE
id = :user_id
";
try
{
// Execute the query
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch(PDOException $ex)
{
// Note: On a production website, you should not output $ex->getMessage().
// It may provide an attacker with helpful information about your code.
die("Failed to run query: " . $ex->getMessage());
}
// Now that the user's rank has changed, the data stored in the $_SESSION
// array is stale; we need to update it so that it is accurate.
$_SESSION['user']['rank'] = $_POST['rank'];
// This redirects the user back to the members-only page after they register
header("Location: private.php");
die("Redirecting to private.php");
}
?>
<h1>Edit Account</h1>
<form action="edit_account.php" method="post">
battletag:<br />
<b><?php echo htmlentities($_SESSION['user']['battletag'], ENT_QUOTES, 'UTF-8'); ?></b>
<br /><br />
Preferred Role:<br />
<input type="radio" name="role" value="Assasin">Assasin
<input type="radio" name="role" value="Warrior">Warrior
<input type="radio" name="role" value="Specialist">Specialist
<input type="radio" name="role" value="Support">Support
<br /><br />
Rank<br />
<input type="text" name="rank" value="<?php echo htmlentities($_SESSION['user']['rank'], ENT_QUOTES, 'UTF-8'); ?>" />
<br /><br />
Password:<br />
<input type="password" name="password" value="" /><br />
<i>(leave blank if you do not want to change your password)</i>
<br /><br />
<input type="submit" value="Update Account" />
</form>
答案 0 :(得分:3)
如果您有多个单选按钮,则可以创建一个返回选中的函数。
function is_checked($db_value, $html_value){
if($db_value == $html_value){
return "checked";
}
else{
return "";
}
}
并使用如下函数:
<input type="radio" name="role" value="Assasin" <?php echo is_checked($db_value, "Assasin"); ?> >Assasin
<input type="radio" name="role" value="Warrior" <?php echo is_checked($db_value, "Warrior"); ?> >Warrior
<input type="radio" name="role" value="Specialist" <?php echo is_checked($db_value, "Specialist"); ?> >Specialist
<input type="radio" name="role" value="Support" <?php echo is_checked($db_value, "Support"); ?> >Support