Sajeeb Ahamed
Sajeeb Ahamed

Reputation: 6390

Delete mysql Table row by clicking submit button php

I have created a cart page from a table named booking .
The cart page has item name and asubmit button named "delete".
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.

I need to delete one item from the table booking by clicking the "delete" button of the item.
How can I do it using 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. How can I do it?


Edit: Here is the delete.php code-

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

Upvotes: 1

Views: 1427

Answers (2)

BigJohn863
BigJohn863

Reputation: 47

Although there is sql injection risks you will need to deal with, this is how you would do it using your current method. Keep in mind you will still need to modify your code to prevent sql injections.

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

Upvotes: 0

Guillaume Lapierre
Guillaume Lapierre

Reputation: 46

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.

If you want to use your form and submit button you could add some Javascript to set an hidden input or use a <button />

Upvotes: 3

Related Questions