显示复选框数据mysql

时间:2013-03-17 17:57:37

标签: php html mysql checkbox show

我有一些小错误,并且想知道是否有人可以提供帮助!

我正在为大学创建一个考勤系统。

此情景:

学生成功登录,然后想要查看他/她参加特定课程的情况。

问题:我希望它向我显示来自mysql的已检查和未检查的数据,它当前显示一个空的复选框(当它要检查时),此时它还显示了大量的重复数据,有可能我可以限制,例如每周显示一条记录,它显示所有数据并重复它。

<?php

$q3= "SELECT attendance.week_number_id, attendance.week_number, courses.course_id, students.student_id, course_attendance.present, course_attendance.notes
 FROM courses, course_attendance, attendance, students
WHERE course_attendance.student_id= ".$_SESSION['student_id']." AND course_attendance.course_id= courses.course_id AND course_attendance.week_id= attendance.week_number_id AND courses.course_id='101'  
 ";


$result = mysql_query($q3)  or die(mysql_error());

echo "<table border='1' align='center'><tr>  <th><strong>Week Number</strong></th> <th><strong>Present</strong></th> <th><strong>Notes</strong></th>  </tr> ";
while($row = mysql_fetch_assoc($result))
{
    extract($row);
echo 

"</td><td  width='200' align='center'>" .$row['week_number'].
"</td><td  width='400' align='center'><input type='checkbox' name='present'" .$row['present']. 
"</td><td  width='400' align='center'>" .$row['notes'].
"</td><tr>";
}
echo "</table>";
?>

注意:我已成功连接到数据库,mysql已启动并正在运行,我正在使用会话,目前它确实为学生显示数据但未显示现有的已检查或未取消的值,复选框为空。

任何人都可以提供帮助

3 个答案:

答案 0 :(得分:0)

在您的代码中,您没有正确定义要检查的复选框。如果'present'字段为1,请创建一个添加checked="true"的代码。

<?php

    $q3 = " SELECT attendance.week_number_id, attendance.week_number, courses.course_id, students.student_id, course_attendance.present, course_attendance.notes 
            FROM courses, course_attendance, attendance, students
            WHERE course_attendance.student_id= ".$_SESSION['student_id']." AND course_attendance.course_id= courses.course_id AND course_attendance.week_id= attendance.week_number_id AND courses.course_id='101'";


    $result = mysql_query($q3) or die(mysql_error());

    echo "
        <table border='1' align='center'>
            <tr>
                <th><strong>Week Number</strong></th>
                <th><strong>Present</strong></th> 
                <th><strong>Notes</strong></th>  
            </tr>
        ";

    while($row = mysql_fetch_assoc($result)) {
        $checked = '';
        if($row['present'] == 1) {
            $checked = ' checked="true"';
        }

        echo "
            <tr>
                <td width='200' align='center'>" . $row['week_number'] . "</td>
                <td width='400' align='center'>
                    <input type='checkbox' name='present'" .$checked . "/>
                </td>
                <td width='400' align='center'>" . $row['notes'] . "</td>
            </tr>
        ";
    }

    echo "</table>";

?>

答案 1 :(得分:0)

你的

echo 
    "</td><td  width='200' align='center'>" .$row['week_number'].
    "</td><td  width='400' align='center'><input type='checkbox' name='present'" .$row['present']. 
    "</td><td  width='400' align='center'>" .$row['notes'].
    "</td><tr>";

声明应该是

$present = "";
if(.$row['present']==1)
{
   $present = "checked =checked/>";    
}
else
{
   $present = "/>";    
 }

回波             “”。$ row ['week_number']。             “”。$ row ['notes']。             “”;

希望这会对你有所帮助。感谢

答案 2 :(得分:0)

$checked = '';
if($present == 1) {
    $checked = 'checked="checked"';
}

echo "</td><td  width='200' align='center'>" .$row['week_number'].
"</td><td  width='400' align='center'><input type='checkbox' name='present'" .$checked . "/>" . 
"</td><td  width='400' align='center'>" .$row['notes'].
"</td><tr>";
}
echo "</table>";

尝试。