php - php 创建多个复选框

标签 php mysql checkbox

我正在尝试创建一个学生出勤表,其中包含每个学生出勤的复选框,该复选框将存储在数据库中。

这是我迄今为止拥有的表,它是通过 ajax 函数调用执行的。大多数数据来自包含学生姓名的数据库。

就本示例而言$numStudent = 5;

echo '<form method="post" action="checkAttend.php"> 
    <table border="1">';
$count = 1;

while(($count < $numStudent) && ($students = mysqli_fetch_assoc($result))) {
    echo '<tr>
        <th>' . $count. '</th>
        <th>' . $students['student_name'] . '</th>
        <th> <input type="checkbox" name="students[]" /> </th>
    </tr>';
    $count++;
}

echo '<th colspan = "3">
    <input type="submit" name="button" id="button" value="Submit" /> </th>';
echo '</table>';
echo '</form>';

checkAttend.php 中,表单完全按照我需要的方式打印,但是当我尝试打印复选框数组中的值时(在将其保存到之前检查其中包含哪些值)数据库)打印出数组中的每个值:

$student_attend[0]       A
$student_attend[1]       r
$student_attend[2]       r
$student_attend[3]       a
$student_attend[4]       y

通常我想在数据库的相应字段中存储某种值来指示学生是否缺勤。

我无法判断我是否正确执行了复选框循环,或者是否需要添加更多内容。

最佳答案

您需要循环遍历 $student_attend 数组,如下所示:

foreach ($student_attend as $value) { 
  // do something with $value
}

有关 foreach 的更多信息 here .

关于php - php 创建多个复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38195196/

相关文章:

php - 显示上次插入数据的数据时出现问题

php - 为什么 mysql 数据只打印电子邮件正文中的最后一行?

mysql - 如何使用 ORDER BY RAND() 优化此 SQL 查询

mysql - 无法连接到docker容器中的MySQL数据库

javascript - 如果按钮没有显示,请更改按钮的文本?

php - mdpf 无法使用 <b> 或 <s> 标签创建 pdf

php - 处理导航栏溢出

mysql - 将逗号分隔的字符串从管道导入到 mysql 数据库

mysql - 使用 Windows 窗体 vb.net 在数据 GridView 中的复选框列的标题中添加一个复选框

C# WinForms : Make CheckBox look like a RadioButton