dongtui2029 2018-07-14 02:30 采纳率: 0%
浏览 87
已采纳

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

In the form below, students are selected from student table in my DB. For each student selected a checkbox is checked if the student is absent and left unchecked if the student is present. The form is later on submitted for it to be inserted in the exam_status table in 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 
",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>

and my action page "action.php" is as follows

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

Now the issue is it doesn't just work as i want. the result always gives the first student absent and the rest present. I have tried all i can and have really researched too but with no success at all. Please anyone around to help me out?

Thanks in advance!

  • 写回答

1条回答 默认 最新

  • dreamlife2014 2018-07-14 03:20
    关注
    <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 
    ",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">
    

    Update your mail file like this. I have changed the form names into a single array. The reason is the checkbox values won't post to the page when the values are not checked. So its not possible to track which one was checked and which is not if you have same name.

    And update your action.php like this,

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

    I have used my own table schema where i have added student_name in exam_status table for better tracking. Now you can see the values updating correctly. Also we can use bulk insert if we need to insert multiple data (Note : I haved used the bulk insert in this answer, i just followed the way you used)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误