douji1999 2014-05-06 17:44
浏览 136
已采纳

如何在PHP中填充MySQL中的复选框?

I'm working with three database tables:

  • Students: Can be assigned zero to many special requirements
  • SpecialRequirements: Names of all special requirements that can be assigned to a student
  • SpecialRequirementAssignments: Includes an associated StudentID and the name of the special requirement

I'm making a page for editing student records. I want to show all the special requirements as checkboxes and check the checkboxes that have previously been assigned to the student. In other words, I want to check the boxes whose values are equal to the values in the SpecialRequirementAssignments table.

I'm getting the following error: "Warning: Invalid argument supplied for foreach()". Have tried my best to use the correct foreach syntax, etc., but it's still not working.

The relevant part of my code. Thanks in advance!

// grab the names of the special requirements
$specialRequirementNamesQuery = "SELECT DISTINCT SpecialRequirementName 
    FROM SpecialRequirements ORDER BY SpecialRequirementName;" ;
$specialRequirementNames = mysql_query($specialRequirementNamesQuery)
    or die(mysql_error());

// grab the names of the special requirements that are selected for this student
$selectedSpecialRequirementsQuery = "SELECT SpecialRequirementName
    FROM SpecialRequirementAssignments
    WHERE StudentID = " . $StudentID . ";" ;
$selectedSpecialRequirements = mysql_query($selectedSpecialRequirementsQuery)
    or die(mysql_error());
$checkedBoxes = mysql_fetch_array($selectedSpecialRequirements);

// create the checkboxes
while ($row = mysql_fetch_array($specialRequirementNames)) {
    echo "<input type='checkbox' name='SpecialRequirementName[]' value='" 
        . $row['SpecialRequirementName'] . "' ";

    // if the SpecialRequirementAssignment record is the same as the SpecialRequirementName record, check the box
    foreach ($checkedBoxes as $value) {
        if($value==$row['SpecialRequirementName']) {
            echo "checked";
        }
    }

        echo " /> " . $row['SpecialRequirementName'] . "<br>";
}

enter image description here

  • 写回答

3条回答 默认 最新

  • dsmvovm27249 2014-05-20 01:55
    关注

    Thanks to the folks who provided helpful pointers. After a short break from the project, I was able to look at it with fresh eyes, and I think I have the answer. It's working for me, anyway.

    //      grab the names of the special requirements
            $specialRequirementNamesQuery = "SELECT DISTINCT SpecialRequirementName 
                FROM SpecialRequirements 
                ORDER BY SpecialRequirementName;" ;
            $specialRequirementNames = mysql_query($specialRequirementNamesQuery)
                or die(mysql_error());
    
    //      create the checkboxes
            while ($srn = mysql_fetch_array($specialRequirementNames)) {            
                echo "<input type='checkbox' name='SpecialRequirementName[]' value='" 
                    . $srn['SpecialRequirementName'] . "' ";
    
    //          grab the names of the special requirements that are selected for this student
                $selectedSpecialRequirementsQuery = "SELECT SpecialRequirementName
                    FROM SpecialRequirementAssignments
                    WHERE StudentID = '" . $StudentID . "';" ;
                $selectedSpecialRequirements = mysql_query($selectedSpecialRequirementsQuery)
                    or die(mysql_error());
    
    //          compare the special requirement name to the selected special requirements
    //          if they're the same, check the box
                while ($checkedBoxes = mysql_fetch_array($selectedSpecialRequirements)) {
                    if(strcmp($srn['SpecialRequirementName'], $checkedBoxes['SpecialRequirementName'])==0) {
                        echo "checked";
                    }
                }
                echo " /> " . $srn['SpecialRequirementName'] . "<br>";
            }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记