RezaM
RezaM

Reputation: 167

Deleting rows with checkbox value

I'm having trouble using the value of a checkbox to delete the rows from the DB. If I select more checkboxes it will only delete one of the selected rows.

My PHP code:

  // 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();

My HTML code:

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

Thank you for your helping!

Upvotes: 1

Views: 71

Answers (1)

Victorino
Victorino

Reputation: 1641

this code is work only with multiple delete not single!

$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();

Upvotes: 3

Related Questions