亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

如何使用複選框刪除 php 和 mysql 的行
P粉982009874
P粉982009874 2023-09-01 22:02:23
0
1
684
<p>我創(chuàng)建了一個(gè)包含資料庫(kù)資訊的表,並嘗試建立複選框以便能夠更輕鬆地刪除行,但有些東西無法正常工作。 </p> <p>我有一個(gè)有表單的按鈕:</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<pan> button> <button type="submit" name="delete" class="btn btn-secondary"><span class="fe fe-trash fe-12 mr-2&quo;span class="fe fe-trash fe-12 mr-2"> ;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」始終為空,因此不會(huì)從資料庫(kù)中刪除任何內(nèi)容。 我該如何解決這個(gè)問題? </p>
P粉982009874
P粉982009874

全部回覆(1)
P粉006540600

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

由於您有兩個(gè)表單,當(dāng)您點(diǎn)擊第一個(gè)表單中的提交按鈕時(shí),它不會(huì)將第二個(gè)表單的資料傳送到伺服器。

因此,請(qǐng)將第二種形式變更為:

<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>

[補(bǔ)充說明]

#如果您想堅(jiān)持使用第一種形式來觸發(fā)刪除操作,那麼請(qǐng):

  1. 在第一種表單中,將刪除從「提交」變更為「按鈕」
  2. 向此刪除按鈕新增 onclick 事件,以便觸發(fā)第二個(gè)表單的提交
  3. 確保在第二種形式中有一個(gè)名為「delete」的隱藏字段,因?yàn)槟付ㄔ?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>
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板