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

时间:2014-05-06 17:44:07

标签: php mysql checkbox

我正在使用三个数据库表:

  • 学生:可以分配零到多个特殊要求
  • 特殊要求:可分配给学生的所有特殊要求的名称
  • SpecialRequirementAssignments:包含关联的StudentID和特殊要求的名称

我正在制作一个用于编辑学生记录的页面。我想将所有特殊要求显示为复选框,并选中之前已分配给学生的复选框。换句话说,我想检查其值等于SpecialRequirementAssignments表中值的框。

我收到以下错误:"Warning: Invalid argument supplied for foreach()"。我已尽力使用正确的foreach语法等,但它仍无效。

我的代码的相关部分。提前谢谢!

// 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 个答案:

答案 0 :(得分:0)

错误意味着$ checkedBoxes不是一个数组,请检查它是否是,或者在foreach之前执行var_dump($checkedBoxes);检查它是否已设置为数组。另外,正如randak所说,更好地切换到PDO,因为mysql_函数已被弃用。

答案 1 :(得分:0)

使用JOIN可以真正简化这段代码;此外,正如之前的评论者所指出的那样,你可以很好地转向mysqli或PDO。

由于我更喜欢​​后者,因此我在这里粗略地概述了代码在一起使用这些工具的方式。请注意,这是粗糙的 - 我没有花太多时间考虑您的具体问题,我只是想说明一种方法。

我对您的数据库模式做了一些假设 - 您可以查看this gist以查看我的查询构建的数据库模式。

try {
    $db = new PDO('mysql:...'); // See php.net/pdo-mysql.connection for specifics
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

    $query = "select SpecialRequirementName as name, case when ra.StudentID = s.StudentID then 'checked=\"checked\"' else '' end as has_assigned
              from SpecialRequirements r 
              left outer join SpecialRequirementAssignments ra on (r.SpecialRequirementID = ra.SpecialRequirementID)
              left outer join Students s on (ra.StudentID = s.StudentID and s.StudentID = :StudentID)";
    $stmt = $db->prepare($query);
    $stmt->bindValue(':StudentID', $StudentID);
    $stmt->execute();
    while($row = $sth->fetch(PDO::FETCH_ASSOC)) {
        echo "<input type='checkbox' name='SpecialRequirementName[]' value='".
        htmlentities($row['name']) . ' ' .
        ($row['has_assigned']) ? :
        '">';
    }
} catch (PDOException $e) {
    // There was a failure somewhere in the above,
    // you can examine $e for details, but should get in the
    // habit of logging these.
}

答案 2 :(得分:0)

感谢那些提供有用指针的人。从项目中短暂休息后,我能够用新鲜的眼睛看着它,我想我有答案。无论如何,这对我有用。

//      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>";
        }