将 'checked' 设置为来自数据库单选输入的值
Set 'checked' to value from database radio input
我一直在努力如何让我的输入单选按钮在编辑帐户页面中设置预设值。从我的数据库中的值查看编辑帐户页面时,我想检查 'role' 输入。
edit: 我想在编辑帐户页面上勾选用户之前选择的单选按钮,所以使用数据库中的值来选择应该勾选的输入。
// 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>
如果您有多个单选按钮,您可以创建一个函数来 return 检查。
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
我一直在努力如何让我的输入单选按钮在编辑帐户页面中设置预设值。从我的数据库中的值查看编辑帐户页面时,我想检查 'role' 输入。
edit: 我想在编辑帐户页面上勾选用户之前选择的单选按钮,所以使用数据库中的值来选择应该勾选的输入。
// 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>
如果您有多个单选按钮,您可以创建一个函数来 return 检查。
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