php - 从mysql中显示所有选中的复选框的值并将其显示在表中

标签 php mysql checkbox

我设计了用户注册页面。它有一个领域的爱好。我在复选框的帮助下设计了该字段。如果用户选择两个或多个爱好,我会将爱好 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/

相关文章:

php - Symfony2 类 Brenne\BaseBundle\Entity\Websites 不是有效的实体或映射的父类(super class)

mysql - Innodb更新锁定

两个值之间的 MySQL WHERE 子句

mysql修复使用哪里;

javascript - 如何根据当前时间显示不同的复选框名称?

PHP MySQL 如何获取列完全唯一的行数

php - 从 Android 设备到 LAMP 服务器的视频流

php - 如何将视频转换为所有可能的编解码器?

javascript - 为什么我的所有复选框不具有相同的行为?

javascript - 复选框 "Error: Unable to get value of the property ' 选中' : object is null or undefined"