如果选中复选框,则删除多行

时间:2009-12-18 12:36:37

标签: php mysql checkbox delete-row

我想构建一个功能,使用户可以调节问题,类似于wordpress评论或典型的电子邮件审核,它使用复选框一次批准/删除许多问题。

如何修改以下代码以检查哪些问题已被检查,并在按下时对删除按钮作出反应?您可能会从以下代码中注意到,我目前可以针对个别问题执行此操作,但我希望为用户提供更多功能。

 <h2><a href="#">Moderate Questions</a></h2>
 <div>
    <button type="button" class="btn" name="delete" id="delete">Delete</button>
    <button type="button" class="btn" name="approve" id="approve">Approve</button>
  <?php

  // Select all unapproved questions in db 
  $sql = "SELECT question_id, question, DATE_FORMAT(question_date, '%e %b %Y at %H:%i') AS dateattime FROM questions WHERE question_approved='0' ORDER BY question_date DESC";
  $result = mysql_query($sql);
  $myquestions = mysql_fetch_array($result);

  if($myquestions) {
    do {
      $question_id = $myquestions["question_id"];
      $question = $myquestions["question"];
      $dateattime = $myquestions["dateattime"];
      echo "<div class='question'>";
      echo "<span value='$question_id'>";
   echo "<input name='checkbox' type='checkbox' id='checkbox' value='$question_id'>";
      echo "$question - <span class='date'>Asked $dateattime </span>";
      echo "</span>\n";
      echo "<div class='question-controls'><a href='".$_SERVER["PHP_SELF"]."?delete=$question_id' title='Delete this question' class='delete' onclick='return confirm(\"Are you sure you want to delete this question?\")'>Delete</a> | <a href='#'>Edit</a> |";
      echo " <a href='".$_SERVER["PHP_SELF"]."?approve=$question_id' title='Publish this question'>Approve</a></div>";
      echo "</div>";
    } while ($myquestions = mysql_fetch_array($result));
  } else {
    echo "<p>No one has asked a question recently.</p>";
  }
  ?>
 </div>

1 个答案:

答案 0 :(得分:1)

您需要修改'checkbox'元素才能生成值数组。代码:

echo "<input name='checkbox[]' type='checkbox' id='checkbox' value='$question_id'>";

之后你可以从php代码调用这个变量(例如使用$ _REQUEST ['checkbox']),它将是数组。所以你可以遍历它并从数据库中删除这些行。