Php 将复选框值数组插入数据库,包括未选中的复选框

Php 将复选框值数组插入数据库,包括未选中的复选框,php,html,mysqli,Php,Html,Mysqli,在下表中,学生从my DB的学生表中选择。对于每个选中的学生,如果该学生缺席,则选中复选框;如果该学生在场,则不选中复选框。稍后提交表单,以便将其插入my DB的考试状态表中 <form method="POST" action="action.php"> <?php $query = "SELECT * from student ORDER BY student_name,student_surname"; $result=mysqli_query($

在下表中,学生从my DB的学生表中选择。对于每个选中的学生,如果该学生缺席,则选中复选框;如果该学生在场,则不选中复选框。稍后提交表单,以便将其插入my DB的考试状态表中

<form method="POST" action="action.php">
<?php
  $query = "SELECT * from student ORDER BY student_name,student_surname";
          $result=mysqli_query($conn,$query);
          if(false===$result)
          {
            printf("error: %s \n",mysqli_error($conn));
          }

          while($row= $result->fetch_assoc()) 
          {
                $studentmatricule = $row['student_matricule'];
                $studentname = $row['student_name'];
                $studentsurname = $row['student_surname'];
?>            
            <div id="studentdiv">
              <label>Matricule</label>
              <input type="text" name="matricule[]" value="<?php echo "$studentmatricule)"; ?>" readonly>

              <label>Name</label>
              <input type="text" name="name[]" value="<?php echo "{$studentname} {$studentsurname}"; ?>" readonly>

              <label > Absent
                  <input type="checkbox" name="absent[]" value="absent" />
              </label>
            </div> <br><br>
        <?php
          }
        ?>
            <input type="submit" name="submit" value="submit">
</form>

矩阵

矩阵

你能分享你的学生和考试状态表模式吗?很乐意帮助:)如果有帮助,请投票。若你们要在mysql表中插入多条记录,也可以尝试使用批量插入。我很想升级投票,但我在这个网站上还是新手。我还没有足够的声望去投票。我理解,希望你能清楚我的答案。如果你有任何疑问,请问我
$matricule = $_POST['matricule'];
$absent=$_POST['absent'];

for ($i=0; $i<sizeof($matricule); $i++)
{    
  if($absent[$i]=='absent')
  {
    $status='absent';
  }else{
    $status='present';
  }
      $query = "INSERT INTO exam_status (student_matricule,status) VALUES ('". $matricule[$i] . "','". $status . "')";
      $result=mysqli_query($conn,$query);
}
<form method="POST" action="action.php">
    <?php
      $query = "SELECT * from student ORDER BY student_name,student_surname";
      $result=mysqli_query($conn,$query);
      if(false===$result)
      {
        printf("error: %s \n",mysqli_error($conn));
      }
      $index = 0;
      while($row= $result->fetch_assoc()) 
      {
            $index++;
            $studentmatricule = $row['student_matricule'];
            $studentname = $row['student_name'];
            $studentsurname = $row['student_surname'];
    ?>            
        <div id="studentdiv">
          <label>Matricule</label>
          <input type="text" name="studenInfo[<?php echo $index; ?>][matriculate]" value="<?php echo $studentmatricule; ?>" readonly>

          <label>Name</label>
          <input type="text" name="studenInfo[<?php echo $index; ?>][name]" value="<?php echo $studentname." ".$studentsurname; ?>" readonly>

          <label > Absent
              <input type="checkbox" name="studenInfo[<?php echo $index; ?>][status]" value="absent" />
          </label>
        </div> <br><br>
    <?php
      }
    ?>
        <input type="submit" name="submit" value="submit">
<?php 
$conn = mysqli_connect("localhost","username","password","db_name"); // update this values as per your configuration
$studenInfo = (!empty($_POST['studenInfo'])) ? $_POST['studenInfo'] : [];
foreach($studenInfo as $value ) {
    $status = (isset($value['status'])) ? 'absent' : 'present';
    $query = "INSERT INTO exam_status (student_name, student_matricule,status) VALUES ('". $value['name'] . "','". $value['matriculate'] . "','". $status . "')";
    $result=mysqli_query($conn,$query);
}
?>