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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?