删除具有复选框值的行

时间:2014-03-01 20:42:28

标签: php html checkbox mysqli

我在使用复选框从数据库中删除行时遇到问题。我不知道如何使用复选框从DB中删除多行。问题是,当我按删除它只会删除一个。

我的PHP代码:

  // Here I didn't know how to put the value of all checkboxes into one variable.
  $intChk = $_POST['chk'];
  /* ..... */
   foreach($intChk as $intRows)
{
    // Starting the process to delete the selected rows.
      $stmt = $mysqli->prepare("DELETE FROM follow WHERE id = ?");
      $stmt->bind_param('s', $intChk);
      $stmt->execute();
      $stmt->close();
    }

我的HTML代码:

<input type="checkbox" id="chk" class="checkbox" value="<?php echo $followId; ?>" name="chk[]">

感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

这样的东西?

<?php
$stmt = $mysqli->prepare("DELETE FROM follow WHERE id = ?");
foreach($_POST['chk'] as $checkbox) {
          $stmt->bind_param('s', intval($checkbox));
          $stmt->execute();
}
$stmt->close();