douhan9467 2016-08-31 05:03
浏览 23
已采纳

如何通过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?

  • 写回答

3条回答 默认 最新

  • dongroufan6846 2016-08-31 05:07
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀