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 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题