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?
JavaScript
x
<?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.
Advertisement
Answer
Before your table:
JavaScript
<form action="" method="post">
and close the form tag after your table.
Place this code as your delete button:
JavaScript
echo '<td><button type="submit" name="deleteItem" value="'.$row['id'].'" />Delete</button></td>"';
In PHP you should do the following
JavaScript
<?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'";
}
?>