I created a table with database information and tried to create checkboxes to be able to delete lines more easily, but something is not working correctly.
I have a button with form:
<form action="delete-register.php" method="post">
<button type="button" class="btn btn-primary"><span class="fe fe-file-plus fe-12 mr-2"></span>New</button>
<button type="submit" name="delete" class="btn btn-secondary"><span class="fe fe-trash fe-12 mr-2"></span>Delete</button>
</form>
And I have rows with checkboxes:
<form action="delete-register.php" method="post">
<td>
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="<?php echo $row['id']; ?>" name="selected[]" value="<?php echo $row['id']; ?>">
<label class="custom-control-label" for="<?php echo $row['id']; ?>"></label>
</div>
</td>
</form>
And there is delete-register.php:
if (isset($_POST['delete'])) {
if (isset($_POST['selected'])) {
foreach ($_POST['selected'] as $id) {
$query = "DELETE FROM registers WHERE id = $id";
mysqli_query($conn, $query);
}
header('Location: registers.php');
exit;
}
}
The problem is that "selected" is always null and so nothing is deleted from the database. How can I solve this problem?