简体   繁体   English

通过单击提交按钮php删除mysql表行

[英]Delete mysql Table row by clicking submit button php

I have created a cart page from a table named booking . 我已经从名为booking的表创建了一个购物车页面。
The cart page has item name and a submit button named "delete". 购物车页面包含item name和名为“删除”的submit按钮。
There has a lot of items in the cart sheet and every item has a "delete" submit type button. 购物车表中有很多项目,每个项目都有一个“删除”提交类型按钮。
The items are inside one form . 这些物品在一个form

I need to delete one item from the table booking by clicking the "delete" button of the item. 我需要通过单击项目的“删除”按钮从表格booking删除一个项目。
How can I do it using PHP . 我怎么能用PHP做到这一点。

    <form action="delete.php" method="POST">
       <div>
           <h2>Item1</h2>
           <input type="submit" name="item1" value="delete">
       </div>
       <div>
           <h2>Item2</h2>
           <input type="submit" name="item2" value="delete">
       </div>
       <div>
           <h2>Item3</h2>
           <input type="submit" name="item3" value="delete">
       </div>
       ...
       <!--There has a lot of items like that-->
    </form>

Now for example I need to delete item100 . 现在举个例子我需要删除item100 How can I do it? 我该怎么做?


Edit: Here is the delete.php code- 编辑:这是delete.php代码 -

<?php      
$query = mysql_query("delete from notification where item = 'item100'") or die(mysql_error());
?>

You do not need form and submit input. 您不需要表单并提交输入。 You could use links: 你可以使用链接:

<a href="delete.php?id=100">delete</a>

This is a GET so you need to perform a redirect to your incoming page to avoid back button problems or use Ajax or both. 这是一个GET,因此您需要执行重定向到传入页面以避免后退按钮问题或使用Ajax或两者。

If you want to use your form and submit button you could add some Javascript to set an hidden input or use a <button /> 如果您想使用表单和提交按钮,可以添加一些Javascript来设置隐藏输入或使用<button />

Although there is sql injection risks you will need to deal with, this is how you would do it using your current method. 虽然您需要处理sql注入风险,但这是您使用当前方法执行此操作的方法。 Keep in mind you will still need to modify your code to prevent sql injections. 请记住,您仍然需要修改代码以防止sql注入。

    <?php $query = mysql_query("delete from notification where item = '".$_POST['name']."'") or die(mysql_error()); ?>

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

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