gpt4 book ai didi

php - 将 'checked' 设置为来自数据库 radio 输入的值

转载 作者:行者123 更新时间:2023-12-05 03:12:19 25 4
gpt4 key购买 nike

我一直在努力如何让我的输入单选按钮在编辑帐户页面中设置预设值。从我的数据库中的值查看编辑帐户页面时,我想检查“角色”输入。

编辑:我想在编辑帐户页面上选中用户之前选择的单选按钮,因此使用数据库中的值来选择应该选中的输入。

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

最佳答案

如果您有多个单选按钮,您可以创建一个函数来返回选中状态。

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

关于php - 将 'checked' 设置为来自数据库 radio 输入的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34012396/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com