Установите ‘флажок’ в значение из радиовхода базы данных

Я изо всех сил пытался сделать так, чтобы в моих переключателях ввода было установлено предустановленное значение на странице редактирования учетной записи. Я хочу, чтобы при просмотре страницы редактирования учетной записи значение из моей базы данных было проверено.

редактировать: я хочу, чтобы переключатель, который был ранее выбран пользователем, проверялся на странице редактирования учетной записи, поэтому используйте значение из базы данных, чтобы выбрать, какой ввод следует проверять.

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

2

Решение

Вы можете создать функцию возврата проверенных, если у вас есть несколько переключателей.

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
3

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector