简体   繁体   English

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

[英]Delete multiple rows if checkbox is checked

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. 我想构建一个功能,使用户能够审核问题,并且与wordpress评论或典型的电子邮件审核类似,该功能使用复选框一次批准/删除许多问题。

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>

You need to modify 'checkbox' element in order to make an array of values. 您需要修改'checkbox'元素以创建值数组。 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. 之后,您可以从php代码中调用此变量(例如,使用$ _REQUEST ['checkbox']),它将是数组。 So your can iterate through it and delete these rows from database. 因此,您可以遍历它并从数据库中删除这些行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM