简体   繁体   English

如何使用php中的post方法删除数据库表中的指定行

[英]How to delete specified row in a database table using post method in php

//Deleting is working. //删除工作。 However, I can't delete the specified row in the table.但是,我无法删除表中的指定行。 It always deletes the last row.它总是删除最后一行。 I hope you could help me.我希望你能帮助我。 Thank you!谢谢! This is my code for displaying data from database:这是我用于显示数据库数据的代码:

<form action="deleteCart.php" method = "post" role="form">
 <?php 
   while ($row = mysqli_fetch_array($result2)) { 
  ?>
    <tr style="text-align: center;">
     <td> <img src="images/<?php echo $row["ImageProduct1"]; ?>"/>  
     <td><?php echo $row['NameProduct1']; ?> </td>
     <td>#<?php echo $row['OrderID']; ?></td>
     <td><?php echo $row['OrderQuantity']; ?></td>
     <td><input type="submit" name="cancelOrder" value = "Cancel" ></td>
     <td><input type="hidden" name="hiddenID" value="<?php echo $row['OrderID']; ?>"></td>
    </tr>
 <?php 
    } 
 ?>  
</form>

//This is my code for deleting: //这是我的删除代码:

if(isset($_POST['cancelOrder'])){
    orderID = $_POST['hiddenID'];
    mysqli_query($con, "DELETE FROM OrderTable WHERE OrderID=$_POST[hiddenID];");
    header('location: deleteCart.php'); 
}

Delete only the last record because you submitting form whole table record.Delete最后一条记录,因为您提交了form整表记录。 you should try this code .你应该试试这个code it will work fine.它会正常工作。

this will submit separate record.这将提交单独的记录。


 <?php 
   while ($row = mysqli_fetch_array($result2)) { 
  ?>
    <form action="deleteCart.php" method = "post" role="form"> 
       <tr style="text-align: center;">
           <td><img src="images/<?php echo $row["ImageProduct1"]; ?>"/>  
           <td><?php echo $row['NameProduct1']; ?> </td>
           <td>#<?php echo $row['OrderID']; ?></td>
           <td><?php echo $row['OrderQuantity']; ?></td>
           <td>
              <input type="hidden" name="hiddenID" value="<?php echo $row['OrderID']; ?>">
              <input type="submit" name="cancelOrder" value = "Cancel" >
           </td>
     


       </tr>
   </form>
 <?php 
    } 
 ?>  

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

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