提交带有多个复选框的单个表单

Submit single form with multiple checkboxes

我用 PHP 做了一个网站。我完成了所有任务,但仍然有问题。 我写这段代码是为了能够在控制面板中删除多个复选框,

有效,但只删除了一个框。

function SelectDelete()
{
        $a = new mysqli("localhost", "root", "", "gamsite");
        if($a->connect_error)
        {
                die("something went worng".$a->connect_error);
        }
        $tmp=$a->query("SELECT * FROM game");
        if($tmp->num_rows>0)
        {
             while($record=$tmp->fetch_assoc())
             {
                ?>

<form  method="post">
<table class="table table-dark">

  <thead>
    <tr>
    <th scope="col"></th>
    <th scope="col"></th>
    <th scope="col"></th>
      <th scope="col">ID</th>
      <th scope="col">Name</th>
      <th scope="col">Download</th>
      <th scope="col">Image</th>
      <th scope="col">video</th>
      <th scope="col">subgame</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th scope="row"></th>
      <th scope="col"></th>
      <input type="hidden" name="act" value="SelectDelete">
      <td>
      <input type="checkbox" name="check[]" value="<?php echo $record['id']; ?>">
      </td>
      <td  name="id"><?php echo $record['id']; ?></td>
      <td name="name"><?php echo $record['name']; ?></td>
      <td name="download"><?php echo $record['download']; ?></td>
      <td name="image"><?php echo $record['image']; ?> </td>
      <td name="video"><?php echo $record['video']; ?></td>
      <td name="subgame"><?php echo $record['subgame']; ?></td>
      <th scope="col"><th scope="col">
    <button type="submit" name="delete" class="btn btn-danger" onclick="location.reload()">Delete</button>
    </th> 
    </tr>
   
  </tbody>
  
</table>
</form>

                 <?php
             }
        }
        else
        {
         echo "There is no data";
        }
if(isset($_POST['delete']))
{
        if(isset($_POST['check']))
        {
             foreach($_POST['check'] as $delete_id)
             {
                $a = new mysqli("localhost", "root", "", "gamsite");
                if($a->connect_error)
                {
                        die("something went worng".$a->connect_error);
                }
                $a->query("DELETE FROM game WHERE id='$delete_id'" );                  
             }   
        }
    }
}

我确实尽了一切努力让它工作,但没有任何效果。在 YouTube 上观看了几个视频,但我必须更改我的代码结构。

您正在对整个表单启动 while 循环。这意味着对于每个提交按钮只有一个复选框。您应该在 HTML 中启动您的表单,然后 运行 您的循环以便有多个复选框。

举个例子:

<form method="post">
    <?php while($record = $tmp->fetch_assoc()): ?>
        // Add the other HTML here, including your checkbox
        <input type="checkbox" name="check[]" value="<?=$record['id']?>">
    <?php endwhile; ?>
</form>

您应该考虑您希望如何显示这些数据。按照目前的情况,每个表单只有一个复选框,因此提交时只会删除一个。