显示复选框数据mysql


show checkbox data mysql

我有一些小错误,想知道是否有人能帮忙!

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

此场景:

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

问题:我希望它显示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已启动并运行,我正在使用会话,目前它确实显示了学生的数据,但没有显示现有的已检查或未检查值,复选框为空。

有人能帮助吗

在代码中,您没有正确定义要选中的复选框。如果"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>";
?>

您的

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

回声"$行['week_number']。"$行[注释]。";

希望这对你有帮助。感谢

$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>";

试试看。