使用复选框值删除行

时间:2014-03-01 19:58:33

标签: php html arrays checkbox

我无法使用复选框的值来删除数据库中的行。 如果我选择更多复选框,它将只删除其中一个选定的行。

我的PHP代码:

  // Here I didn't know how to put the value of all checkboxes into one variable.
  $intChk = array();
  $intChk = $_POST['chk'];
  /* ..... */
          $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 :(得分:3)

此代码仅适用于多个删除而不是单个!

$params = array();
foreach ($_POST['chk'] as $val) {
    $intChk[] =  $val;          
}

$params = array_map(function($check) {
    return "?";
}, $intChk);

$params = implode(',',$params);

$stmt = $mysqli->prepare("DELETE FROM follow WHERE id IN (".$params.")");

foreach ($intchk as $key => $val) {
    $stmt->bind_param($key+1, $val);
}

$stmt->execute();
$stmt->close();
相关问题