在PHP中使用复选框值处理数组

我的代码出现问题,就是在存储复选框值的数组中出现未定义的偏移量。 我可能做错了什么,但是我的目标是存储是否选中了一个复选框,然后使用它来验证事情。 这是主页中的代码:

<form method="POST" action="adminmoderationbackend.php">
   <table class="table">
      <thead class="thead-dark">
          <tr>
              <th>Username</th>
              <th>Block</th>
          </tr>
      </thead>
   <tbody id="myTable1">
      <?php 
          $get_users_query = "SELECT userUsername, userBlocked FROM user";
          $get_users_result = mysqli_query($db, $get_users_query);
          while ($get_users = mysqli_fetch_array($get_users_result))
          {
              echo '<tr>
                 <td style="display:none;"><input type="text" name="userUsername[]" value="' . $get_users['userUsername'] . '"></td>
                 <td>' . $get_users['userUsername'] . '</td>';
                 if($get_users['userBlocked'] == "NO"){
                     echo '<td><input style="transform: scale(1.5)" type="checkbox" id="userBlocked1" name="userBlocked[]" value="Blocked"></td>';
                 }else if($get_users['userBlocked'] == "YES"){
                     echo '<td><input style="transform: scale(1.5)" checked type="checkbox" id="userBlocked1" name="userBlocked[]" value="Blocked"></td>';
                 }
             '</tr>';
          }
     ?>
</tbody>
</table>
  <div class="container">
   <div class="row">
     <div class="col text-center">
        <button type="submit" name="blockbtn" style="background:#009BE1; color:#FEFC5C;" class="btn text-center">Confirm</button>
     </div>
    </div>
  </div>
</form>

这是php后端页面:

$total = count($_POST['userUsername']);
if(empty($_POST['userUsername'])){
   header("Location: adminmoderation.php");
}else{
   $userUsername_arr = $_POST['userUsername'];
   $userBlocked_arr = $_POST['userBlocked']; 
}
for($i = 0; $i < $total; $i++){
   $userUsername_update = $userUsername_arr[$i]; 
   $userBlocked_update = $userBlocked_arr[$i];       //Error points to this line
   if($userBlocked_update == "Blocked"){
     $userBlocked_update = "YES";
   }else{
     $userBlocked_update = "NO";
   }
   $get_data_query = "SELECT userUsername, userBlocked FROM user WHERE userUsername = '$userUsername_update'";
   $get_data_result = mysqli_query($db, $get_data_query);
   $get_data = mysqli_fetch_assoc($get_data_result);
   if($get_data['userBlocked'] == $userBlocked_update){
   }else{
      $query = "UPDATE user SET userBlocked = '$userBlocked_update' WHERE userUsername = '$userUsername_update'";
      mysqli_query($db, $query);
   }
   $userUsername_update = "";
   $userBlocked_update = "";
}

代码中可能包含一些额外的信息,这些信息对于解决此问题没有用,但是我想确保我提供的最大代码能够帮助解决此问题。 谢谢。

评论