首页  >  问答  >  正文

如何使用复选框删除 php 和 mysql 的行

<p>我创建了一个包含数据库信息的表,并尝试创建复选框以便能够更轻松地删除行,但有些东西无法正常工作。</p> <p>我有一个带有表单的按钮:</p> <pre class="brush:php;toolbar:false;"><form action="delete-register.php" method="post"> <button type="button" class="btn btn-primary"><span class="fe fe-file-plus fe-12 mr-2"></span>New</button> <button type="submit" name="delete" class="btn btn-secondary"><span class="fe fe-trash fe-12 mr-2"></span>Delete</button> </form></pre> <p>我有带有复选框的行:</p> <pre class="brush:php;toolbar:false;"><form action="delete-register.php" method="post"> <td> <div class="custom-control custom-checkbox"> <input type="checkbox" class="custom-control-input" id="<?php echo $row['id']; ?>" name="selected[]" value="<?php echo $row['id']; ?>"> <label class="custom-control-label" for="<?php echo $row['id']; ?>"></label> </div> </td> </form></pre> <p>还有delete-register.php:</p> <pre class="brush:php;toolbar:false;">if (isset($_POST['delete'])) { if (isset($_POST['selected'])) { foreach ($_POST['selected'] as $id) { $query = "DELETE FROM registers WHERE id = $id"; mysqli_query($conn, $query); } header('Location: registers.php'); exit; } }</pre> <p>问题是“selected”始终为空,因此不会从数据库中删除任何内容。 我该如何解决这个问题?</p>
P粉982009874P粉982009874436 天前558

全部回复(1)我来回复

  • P粉006540600

    P粉0065406002023-09-02 18:45:55

    Please note that the data submitted will be within the scope of <form>....</form>

    由于您有两个表单,当您点击第一个表单中的提交按钮时,它不会将第二个表单的数据发送到服务器。

    因此,请将第二种形式更改为:

    <form action="delete-register.php" method="post">
    
          <div class="custom-control custom-checkbox">
           <input type="checkbox" class="custom-control-input" id="<?php echo $row['id']; ?>" name="selected[]" value="<?php echo $row['id']; ?>">
           <label class="custom-control-label" for="<?php echo $row['id']; ?>"></label>
         </div>
    
    <input type=submit name=delete>
    
    </form>

    [补充说明]

    如果您想坚持使用第一种形式来触发删除操作,那么请:

    1. 在第一种表单中,将删除从“提交”更改为“按钮”
    2. 向此删除按钮添加 onclick 事件,以便触发第二个表单的提交
    3. 确保在第二种形式中有一个名为“delete”的隐藏字段,因为您指定在 PHP 脚本中包含此字段
    4. you may have noticed that I have added id=form2 in the 2nd form so as to facilitate triggering of the submission by form1

    这是修改后的代码:

    <form method="post">
       <button type="button" class="btn btn-primary"><span class="fe fe-file-plus fe-12 mr-2"></span>New</button>
    
       <button type="button" name="delete" class="btn btn-secondary"
    onclick='document.getElementById("form2").submit()';
       ><span class="fe fe-trash fe-12 mr-2"></span>Delete</button>
    
    </form>
    
    
    
    <form id=form2 action="delete-register.php" method="post">
    
       <div class="custom-control custom-checkbox">
           <input type="checkbox" class="custom-control-input" id="<?php echo $row['id']; ?>" name="selected[]" value="<?php echo $row['id']; ?>">
           <label class="custom-control-label" for="<?php echo $row['id']; ?>"></label>
         </div>
    
    <input type=hidden name=delete value="delete">
    
    </form>

    回复
    0
  • 取消回复