【问题标题】:Submit single form with multiple checkboxes提交带有多个复选框的单个表单
【发布时间】:2021-06-25 23:49:49
【问题描述】:

我用 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 上观看了几个视频,但我必须更改我的代码结构。

【问题讨论】:

  • 不要建立多个数据库连接。您的脚本应该只连接一次,然后将该连接重新用于所有查询。您的删除查询不安全/不稳定,因为您没有使用准备好的语句。 mysqli 结果集对象(在您的情况下为$tmp)可以立即输入foreach(),并被视为关联数组的索引数组,无需任何fetch 调用。 stackoverflow.com/a/66775416/2943403 我建议更合理地命名您的连接变量——$a 不是一个好的连接名称。将连接传递给您的自定义函数SelectDelete($conn)
  • 我也不喜欢onclick="location.reload()"&lt;input type="hidden" name="act" value="SelectDelete"&gt; 的样子。我认为那些应该消失。我认为您的die() 文本中有错字——应该是smoethnig newt

标签: php html forms checkbox


【解决方案1】:

您正在整个表单上开始您的 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>

您应该考虑如何显示这些数据。就目前而言,每个表单只有一个复选框,因此提交时只会删除一个。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-03
    相关资源
    最近更新 更多