0

在下面的表格中,学生是从我的数据库中的学生表中选择的。对于每个选择的学生,如果学生缺席,则选中一个复选框,如果学生在场,则不选中。稍后提交该表单,以便将其插入我数据库的exam_status表中。

<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>

我的动作页面“ action.php ”如下

$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);
}

现在的问题是它不仅仅像我想要的那样工作。结果总是让第一个学生缺席,其余学生在场。我已经尽我所能,并且也进行了真正的研究,但根本没有成功。请身边的人帮帮我?

提前致谢!

4

1 回答 1

0
<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">

像这样更新您的邮件文件。我已将表单名称更改为单个数组。原因是未选中值时复选框值不会发布到页面。因此,如果您的姓名相同,则无法跟踪哪个被检查了,哪个没有被检查。

并像这样更新您的 action.php,

<?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);
}
?>

我使用了我自己的表模式,我在exam_status 表中添加了student_name 以便更好地跟踪。现在您可以看到正确更新的值。如果我们需要插入多个数据,我们也可以使用批量插入(注意:我在这个答案中使用了批量插入,我只是按照你使用的方式)

于 2018-07-14T03:20:37.617 回答