HurkNburkS
HurkNburkS

Reputation: 5510

adding a delete button in php

I have created a table of items that are in my DB, it comes out perfectly however I would now like to add a delete button in another column of the data I am outputting.

However I am just not sure how to do it, I do have a uniqueid for each of the tables so could I use that as the id or what ever you would call it of the button?

<?php

//Start session
    //... all the connection stuff here


$result = mysql_query("SELECT * FROM feathermattresstoppers");

echo "<table border='1'>
<tr>
<th>name</th>
<th>old price</th>
<th>price</th>
<th>delete</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['name'] . "</td>";
  echo "<td>" . $row['old_price'] . "</td>";
  echo "<td>" . $row['price'] . "</td>";
echo "<td>"<!-- how could I add a button here? -->"</td>";
  echo "</tr>";
  }
echo "</table>";

?>

any help would be appreciated.

Upvotes: 3

Views: 40618

Answers (7)

Ahmet Ozisik
Ahmet Ozisik

Reputation: 443

Before your table:

<form action="" method="post">

and close the form tag after your table.

Place this code as your delete button:

echo '<td><button type="submit" name="deleteItem" value="'.$row['id'].'" />Delete</button></td>"';

In PHP you should do the following

<?php

if(isset($_POST['deleteItem']) and is_numeric($_POST['deleteItem']))
{
  // here comes your delete query: use $_POST['deleteItem'] as your id
  // $delete = $_POST['deleteItem']
  // $sql = "DELETE FROM `tablename` where `id` = '$delete'"; 
}
?> 

Upvotes: 13

Jaicy Joseph
Jaicy Joseph

Reputation: 33

    echo '<td><a href="yourpagename.php?delete=1&id='.$row["stud_no"].'"><button type="submit" style="font-weight:bold;" name="delete" class="btn btn-danger"><span class="glyphicon glyphicon-trash"></span> Delete</button></a></td>';

You can put your button inside the tag. And cover it will tag and in href attribute you can put your page name in which the url will have the is displayed in it. Like delete=1 is used, so that you can set this button and put &id which shows nd stores the id in it. So that in that same page or next page you can get that id with get or post method. For example i have used id=$row['stud_no'] this will display the id which you have clicked nd id will be displayed in url and then in that page or next page, you can take $id = $_POST['id']; by doing this you can get your stud_no in $id. to see whether you got the proper id , you can echo $id and see it.

Upvotes: -1

Sheena
Sheena

Reputation: 16212

the simplest way would be to enclose each tr in a form and add the unique key as a hidden field.

while($row = mysql_fetch_array($result))
{
echo '<form action="here_maybe" method="POST" >'
echo '<input name="row_id" type="hidden" value="'.$row['id'].'"/>'
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['old_price'] . "</td>";
echo "<td>" . $row['price'] . "</td>";
echo "<td><input type="submit" value="Delete" name="delete_command"/></td>";
echo "</tr>";
echo "</form>"
}

Upvotes: 0

Arun Killu
Arun Killu

Reputation: 14233

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['name'] . "</td>";
  echo "<td>" . $row['old_price'] . "</td>";
  echo "<td>" . $row['price'] . "</td>";
echo "<td>"<a href="delete.php?<?php echo $row['id'];  ?>">delete</a></td>";

  echo "</tr>";
  }
echo "</table>";

?>

and in your delete.php

if(isset($_REQUEST['id']) && is_numeric($_REQUEST['id']))
{
 //query for delete.
}

//w3c says you shouldn't use get method for any operation other than retrieval from data base so better method is to use post .

Upvotes: 0

Sherin Jose
Sherin Jose

Reputation: 2526

Try this code,

<?php
//codes 
echo "<td><form name='frmDelete' action='your delete page here' method='post'><input type='hidden' name='itemid' value='{echo ID here}'><input type='submit' name='dlteBtn' value='delete'></form></td>";
//codes
?>

In the delete page

<?php
if(isset($_POST['dlteBtn'])){
 $id=$_POST['itemid'];
 //delete query for row with id,  $id
 }
?>

Upvotes: 0

arkascha
arkascha

Reputation: 42915

Two approaches:

1.) you code a small html form for each line in the table. That form contains the lines ID in a hidden input file alongside the button. In your processing php code you get the information that the delete button has been pressed together with the ID of the line to be deleted.

2.) you code that in a dynamic way using javascript. Then you simply code the same delete button in all lines of your table. In addition you bind a short javascript function to the 'click' event of all those buttons. If one is clicked your function is called and it can identify the line the clicked button belongs to by looking for the buttons parent element and reading its ID. Then you post back that ID.

Upvotes: 1

ScoRpion
ScoRpion

Reputation: 11474

Yes, But you will need to put this table into a form. You can create a button which on click will submit the form (check boxes) of the table rows, and your checkbox id will be the unique id from database row, then you can simply delete the rows with submitted IDs

Upvotes: 1

Related Questions