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

发布于 2024-08-15 03:14:31 字数 1840 浏览 4 评论 0原文

我想构建一个功能,使用户能够审核问题,类似于 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>

I want to build a feature that enables the user to moderate questions and is similar to wordpress comment or typical email moderation that uses a checkbox to approve/delete many questions at once.

How would I modify the following code to check which the questions are checked and react to the delete buttons when pressed? You may notice from the below code that I can currently do this with individual questions but I want to give the user more power.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伏妖词 2024-08-22 03:14:31

您需要修改“checkbox”元素才能创建值数组。代码:

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

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

You need to modify 'checkbox' element in order to make an array of values. Code:

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

After that your can call this variable from php code(using $_REQUEST['checkbox'] for example) and it will be array. So your can iterate through it and delete these rows from database.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文