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

时间:2011-11-01 18:48:38

标签: php mysql html

我有一个显示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;
    } 
  }

然而,当我提交表单时没有任何反应,没有显示任何内容,也没有删除任何内容。是否有人可能导致我的脚本无法正常工作?

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

2 个答案:

答案 0 :(得分:1)

if (!resultat) {

应该是

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

'休息'电话是多余的。 die()将杀死脚本,因此无法达到中断。

您是否确认已向服务器发送了某些内容。 var_dump($_POST)显示什么?

答案 1 :(得分:0)

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

http://pastebin.com/KdqscV87

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

我从循环中删除了隐藏的(class="invisible")表单(表单中没有表单)。我不确定你在那里需要什么,但它不会在另一种形式内工作。

相关问题