简体   繁体   English

删除php mysql中的选定行列表

[英]delete selected rows list in php mysql

I'm trying to delete the selected rows from the list I have in mysql so I won't have to delete the rows in my table one by one.我正在尝试从 mysql 中的列表中删除选定的行,这样我就不必一一删除表中的行。 When I press delete, my page refreshes without any php error but I don't get the result desired either.当我按删除时,我的页面刷新而没有任何 php 错误,但我也没有得到想要的结果。 here is what I have:这是我所拥有的:

<?php $product_set = find_all_products(); ?>

<body>
<form action="manage_products.php" method="delete">
<div class="page">
  <article>
<div id="page">
<?php echo message(); ?>
<h2>Manage Products</h2>
<table>
  <tr>
    <th style="text-align: left; width: 125px;">Location</th>
    <th style="text-align: left; width: 250px;">URL to the Product</th>
    <th style="text-align: left; width: 100px;">First Name</th>
    <th style="text-align: left; width: 100px;">Last Name</th>
    <th style="text-align: left; width: 100px;">Size</th>
    <th style="text-align: left; width: 100px;">Status</th>
    <th style="text-align: left; width: 100px;">Checked</th>
    <th colspan="2" style="text-align: left;">Actions</th>
  </tr>
<?php while($product = mysqli_fetch_assoc($product_set)){ ?>

  <tr>
    <td><?php echo htmlentities($product["location"]);?></td>
    <td><?php echo htmlentities($product["productURL"]); ?></td>
    <td><?php echo htmlentities($product["fname"]); ?></td>
    <td><?php echo htmlentities($product["lname"]); ?></td>
    <td><?php echo htmlentities($product["size"]); ?></td>
    <td><?php echo htmlentities($product["status"]); ?></td>
    <td><input name="checkbox" type="checkbox" id="checkbox[]" value="<?php echo $product['id']; ?>"></td>
    <td><a href="edit_order.php?id=<?php echo urlencode($product["id"]); ?>">Edit Order</a></td>
    <td><a href="delete_product.php?id=<?php echo urlencode($product["id"]); ?>" onclick="return confirm('Are you sure?');">Delete Order</a></td>
  </tr>
<?php } ?>
</table>

  <?php

  // Check if delete button active, start this

  if(isset($_GET['delete']))
  {
      $checkbox = $_GET['checkbox'];

      for($i=0;$i<count($checkbox);$i++){

          $del_id = $checkbox[$i];
          $sql = "DELETE FROM product WHERE link_id='$del_id'";
          $result = mysql_query($sql);
      }
// if successful redirect to delete_multiple.php
      if($result){
          $_SESSION["message"] = "Product deletion failed.";
          redirect_to("created_products.php");          }
  }

  //mysqli_close($dbc);

  ?>

<br />
</div>
</div>
<br>
  <input type="submit" name="submit" value="Delete" onclick="return val();"/>
  </form>
    <div class="clear-all"></div>
  </article>
</div>
</body>

Also place all the code which is for deleting the rows in the starting of your file.还将所有用于删除行的代码放在文件的开头。 Thank you @Traveller for this.感谢@Traveller 为此。

Instead of for loop, use WHERE IN使用WHERE IN代替 for 循环

for($i=0;$i<count($checkbox);$i++){

      $del_id = $checkbox[$i];
      $sql = "DELETE FROM product WHERE link_id='$del_id'";
      $result = mysql_query($sql);
  }

Replace that with将其替换为

$ids = implode(",", $_POST['checkbox']);
$sql = "DELETE FROM product WHERE link_id in ($ids)";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM