将多个复选框值保存到数据库中,但只保存最后一个值

时间:2017-05-10 01:29:36

标签: php mysql

这是我的表格

      <?php  
      if(isset($_POST['sub']))  
      {  
      $host="localhost";//host name  
      $username="root"; //database username  
      $word="";//database word  
      $db_name="meeting";//database name  
      $tbl_name="employee"; //table name  
      $con=mysqli_connect("$host", "$username", "$word","$db_name")or die("cannot connect");
      $checkbox1=$_POST['atte'];  
      $chk="";  
      foreach($checkbox1 as $chk1)  
         {  
            $chk = $chk1;  
            $in_ch=mysqli_query($con,"update employee SET attendance = ''");  

      $in_ch=mysqli_query($con,"update employee SET attendance='present' where id='$chk'");  
         } 


      if($in_ch==1)  
         {  
          echo '<script language="javascript">';
            echo 'alert("Attendance Successfully Saved!")';
            echo '</script>';


         }  
      else  
         {  
            echo'<script>alert("Not saved. Try again!")</script>';  
         }  
      }  
      ?>

这是我的PHP

   sudo docker run -v "`pwd`/data_stg:/var/lib/pgsql/data:Z" -e PGPORT=5433 -e POSTGRESQL_USER=user1 -e POSTGRESQL_PASSWORD=password -e POSTGRESQL_DATABASE=test_db -d -p 5433:5433 fedora/postgresql

代码只保存最后一个值。

鉴于用户选择了多个复选框。

如何使用UPDATE查询将多个值保存到mysql

1 个答案:

答案 0 :(得分:-1)

尝试像这样更改foreach循环:

foreach($checkbox1 as $chk)  
     {  
        $in_ch=mysqli_query($con,"update employee SET attendance = ''");  
        $in_ch=mysqli_query($con,"update employee SET attendance='present' where id=$chk");  
     }