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

모든 응답(1)
P粉006540600

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>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!