Alex Parser Alex Parser - 3 months ago 16
MySQL Question

How to delete multiple rows through checkboxes in php

I want to delete multiple rows that are selected in tables when I press Delete button my code is this

index.php

<?php
if(isset($_POST['Delete']))
{
for($i=0;$i<count($checkbox);$i++){
$ids= $checkbox[$i];
$query2="DELETE FROM products WHERE serialid='$ids'";
mysqli_query($conn,$query2);
}
}
$query2="SELECT * FROM products";
$allresult = mysqli_query($conn,$query2);
?>
<form method="POST" action="index.php">
<input type="button" class="btn btn-danger" name="Delete" value="Delete"/>
</form>
<table class="table table-striped table-hover table-responsive" id="inventoryTable">
<thead>
<tr>
<td>Serial ID</td>
<td>Name</td>
<td>Manufacturer</td>
<td>Keys</td>
<td>Description</td>
<td>Category</td>
<td>Block</td>
<td>Floor</td>
<td>Room</td>
</tr>
</thead>
<tbody>
<?php while($row = mysqli_fetch_array($allresult)) { ?>
<tr>
<td><input name="checkbox[]" type="checkbox" value="<?php echo $row['serialid']; ?>"></td>
<td><?php echo $row['serialid']?></td>
<td><?php echo $row['name'] ?></td>
<td><?php echo $row['manufacturer'] ?></td>
<td><?php echo $row['licensekeys'] ?></td>
<td><?php echo $row['description'] ?></td>
<td><?php echo $row['categoryname'] ?></td>
<td><?php echo $row['block'] ?></td>
<td><?php echo $row['floor'] ?></td>
<td><?php echo $row['room'] ?></td>
</tr>
<?php }?>
</tbody>
</table>


But whenever I select rows and press delete button nothing happens. Is there anything am I missing?

Answer

The usage of form is entirely wrong on your HTML page. The form should appear as how i have done. Input type button is wrong you have to change to submit..

<?php 
if(isset($_POST['Delete']))
{
for($i=0;$i<count($_POST['checkbox']);$i++){
    $ids= $_POST['checkbox'][$i];
    $query2="DELETE FROM products WHERE serialid='".$ids."'";
    mysqli_query($conn,$query2);
}
}
$query2="SELECT * FROM products";
$allresult = mysqli_query($conn,$query2);
?>
<form method="POST" action="index.php">
<input type="submit" class="btn btn-danger" name="Delete" value="Delete"/>
<table class="table table-striped table-hover table-responsive" id="inventoryTable">
    <thead>
    <tr>
        <td>Serial ID</td>
        <td>Name</td>
        <td>Manufacturer</td>
        <td>Keys</td>
        <td>Description</td>
        <td>Category</td>
        <td>Block</td>
        <td>Floor</td>
        <td>Room</td>
    </tr>
    </thead>
    <tbody>
            <?php while($row = mysqli_fetch_array($allresult)) { ?>
   <tr>
    <td><input name="checkbox[]" type="checkbox" value="<?php echo $row['serialid']; ?>"></td>
      <td><?php echo $row['serialid']?></td>
     <td><?php echo $row['name'] ?></td>
      <td><?php echo $row['manufacturer'] ?></td>
      <td><?php echo $row['licensekeys'] ?></td>
      <td><?php echo $row['description'] ?></td>
    <td><?php echo $row['categoryname'] ?></td>
    <td><?php echo $row['block'] ?></td>
    <td><?php echo $row['floor'] ?></td>
    <td><?php echo $row['room'] ?></td>
</tr>
<?php }?>
    </tbody>
</table>
</form>

Wrap all the details in the <form> element.

Comments