我设计了用户注册页面。它有一个领域的爱好。我在复选框的帮助下设计了该字段。如果用户选择两个或多个爱好,我会将爱好 ID 保存在数据库中。现在我需要显示特定用户在其个人资料编辑页面中选择的所有爱好名称。我完成了以下编码,但它显示数据库中的复选框名称而未选择。应选中用户选择的复选框名称。但它并没有这样做。请提供任何帮助。
<?php
$query = "SELECT * FROM hobbies";
$result = mysqli_query($con, "$query");
while ($r=mysqli_fetch_array($result)){
$hobbyid=$r["hobby_id"];
$hobbyname=$r["hobby_name"];
if($hobby==$r[0]){ //Here $hobby is the hobby id which is entered by the user when registration. That hobby id is saved in the db.
echo "<input type='checkbox' checked name='check_list[]' value='echo $hobbyid;'> $hobbyname";
}else{
echo "<input type='checkbox' name='check_list[]' value='echo $hobbyid;'> $hobbyname";
}
}
?>
最佳答案
Try again with below code segment. I have modified your code.
<?php
$query = "SELECT * FROM hobbies";
$result = mysqli_query($con, "$query");
while ($r=mysqli_fetch_array($result)) {
$hobbyid = $r["hobby_id"];
$hobbyname = $r["hobby_name"];
$isChecked = '';
if($hobby == $r["hobby_id"]) { //Here $hobby is the hobby id which is entered by the user when registration. That hobby id is saved in the db.
$isChecked = "checked='checked'";
}
echo "<input type='checkbox' $isChecked checked name='check_list[]' value='$hobbyid;'/> $hobbyname";
}
?>
关于php - 从mysql中显示所有选中的复选框的值并将其显示在表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37023540/