在php中使用复选框和表单删除数据库中的元素


Delete elements from database using checkboxes with forms in php

我有一个表,显示mysql表的信息,在最后一列我有一个这样的复选框:

<form method="POST" action="php/delete.php">
  <thead>
    <tr
      <th>Nom</th>
      <th>Espéce</th>
      <th>Cri</th>
      <th>Propiétaire</th>
      <th>Age (années)</th>
      <th><input type="submit" name="supprimer" value="Supprimer" /></th>
    </tr>
  </thead>
  <tbody>
    <?php
      $connexion = mysql_connect($hote, $login, $mdp);
      mysql_select_db($bd, $connexion);
      $req = "Select * from animaux;";
      $resultat = mysql_query($req, $connexion);
      while (list($id, $nom, $esp, $cri, $prop, $age) = mysql_fetch_row($resultat)) {
    ?>
      <tr>
        <td><?= $nom ?> </td>
        <td><?= $esp ?></td>
        <td><?= $cri ?></td>
        <td><?= $prop ?></td>
        <td><?= $age ?></td>
        <td> <input type="checkbox" name="choix[]" value=<?= $id ?>></td>
      </tr>
  <?php } ?>
  </form>

我使用以下脚本删除行:

  include("../BD/bd_params.inc.php");
  $connexion = mysql_connect($hote,$login,$mdp);
  mysql_select_db($bd, $connexion);
  $del = $_POST['choix'];
  foreach ($del as $val) {
    $req = "delete from `animaux` where id = '$val'";
    $resultat = mysql_query($req,$connexion);
    if (!resultat) {
      die('Requête invalide : ' . mysql_error());
      break;
    } 
  }

然而,当我提交表单什么也没有发生,什么也没有显示,什么也没有被删除。有没有人可能导致我的脚本不工作?

(哦,对不起我的英语…)

if (!resultat) {
应该

if (!$resultat) {
     ^--missing $

'break'调用是多余的。Die()将终止脚本,因此不可能到达断点。

您是否验证有东西正在发送到服务器。var_dump($_POST)表示什么?

我已经在这里创建了一个新版本的pastebin:

http://pastebin.com/KdqscV87

基本上,您希望在开始循环之前打开表单,并在完成循环后关闭表单。

我已经从循环中删除了隐藏的(class="invisible")表单(您不能在表单中拥有表单)。我不知道你需要什么,但它不会在其他形式内工作。