3

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.

1
  • u can do this in another way like, instead putting the button put the anchor tag there, on onclick event of the anchor tag call the ajax function and pass the respective tableId to the function. Ajax function will refer some php page and pass the tableId to the php page. Using this tableId u can delete the record Commented Oct 1, 2012 at 6:07

7 Answers 7

13

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'"; 
}
?> 
Sign up to request clarification or add additional context in comments.

9 Comments

This is a straight-forward example and actually you will have ID's as button labels. There is another way of doing this: creating links on the fly. echo something like '<a href="yourpage.php?deleteItem='.$row['id'].'">Delete</a>'; and in your php just replace $_POST with $_GET.. You don't have to create a form in this example.
right.. just wondering if your code example has the quote marks in the correct place or not? its messing up my code color
Which example do you mean ? Use above example simply after an echo: <?php echo '<a href="yourpage.php?deleteItem='.$row['id'].'">Delete</a>'; ?>
This part **echo '<td><input type="submit" name="deleteItem" value="'.$row['id'].'" /></td>"; ** its causing the rest of my code to comment out weirdly in my editor..
Just a ** ' ** is missing there. Fixed it in my answer. Sorry :)
|
1

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

Comments

1

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.

Comments

0

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

Comments

0
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 .

Comments

0

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>"
}

3 Comments

That's actually not valid HTML -- a <table> cannot directly contain block elements like <form>. It'd have to go into the form cell.
@duskwuff: Thanks. I've used this sort of thing before loads of times without any issues so all the major browsers support this
Most browsers will "rewrite" the HTML at runtime to move the <form> element to a legal location; the results are visible in a DOM inspector. In many cases, this will end up resulting in forms that don't behave the way you expected.
-1
    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.

3 Comments

could you provide any explanation of your code sample?
Yes. In particular why you chose to put the <button> in an <a>.
@MrLister So that button when cicked will go to the particular page, which i kept in <a> tag with the url in those particular page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.