删除php mysql中的选定行列表

时间:2015-10-26 14:32:28

标签: php mysql

我试图从mysql中的列表中删除所选行,因此我不必逐个删除表中的行。当我按删除时,我的页面刷新没有任何PHP错误,但我也没有得到所需的结果。这就是我所拥有的:

<?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>

1 个答案:

答案 0 :(得分:0)

同时放置所有用于删除文件开头行的代码。谢谢@Traveller。

使用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);
  }

将其替换为

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