php - 显示复选框数据 mysql

标签 php html mysql checkbox show

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

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

这个场景:

学生成功登录,然后想查看他/她对特定类(class)的出勤率。

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

谁能帮忙

最佳答案

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

?>

关于php - 显示复选框数据 mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15464171/

相关文章:

php - Nginx 响应时间

php - MySQL 获取过去 12 个月按月分组的数量

android - 如何更改本地 HTML 文件的装饰以在 Android 的 WebView 上显示它

mysql - Zend 框架 1.11 - SQLSTATE[HY000] [2003] 无法连接到 'localhost' (111) 上的 MySQL 服务器

php - 选择具有条件的给定行前后的 N 行

html - StencilJS:可选的 <slot/> 元素在 IE11/Edge 中不起作用

javascript - 通过 Jquery 为动态高度为 : Not working in IE11 的标签设置 "!important"

基于字母代码的 PHP/SQL ID

java - 使用Datanucleus JDO的Servlet找不到MySQL驱动程序

javascript - Ajax javascript php