Interoth Posted December 9, 2011 Share Posted December 9, 2011 Hi, I'm new to the forum so this could go in the MySQL section but I'm not sure. I am trying to make a page that will list all records from a column in HTML table and have a delete button to remove a specific record. I have got to the part where I have listed the records in a table. Note: Only records from a specific column ('links') are printed. Here is the code: $con = mysql_connect("localhost","***","***"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("***", $con); $result = mysql_query("SELECT * FROM main"); echo "<table border='1'> <tr> <th>Current links</th> </tr>"; while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['links'] . "</td>"; echo "</tr>"; } echo "</table>"; mysql_close($con); How would I go about adding a delete button next to each record to delete that specific record? Thanks for any help. Quote Link to comment https://forums.phpfreaks.com/topic/252854-delete-separate-mysql-records-with-a-button/ Share on other sites More sharing options...
ddubs Posted December 9, 2011 Share Posted December 9, 2011 When I get to something like this its best to use an anchor tag (which you can style to look like a button or icon). Basically in your table you'd generate a <td><a href="?action=delete&id=13">Delete</a></td> cell and you click this link to delete the id from the database. This can redirect back to itself or a seperate script for deletion (where you confirm, etc..). Anyways, hope this helps! Quote Link to comment https://forums.phpfreaks.com/topic/252854-delete-separate-mysql-records-with-a-button/#findComment-1296374 Share on other sites More sharing options...
litebearer Posted December 9, 2011 Share Posted December 9, 2011 Presuming your db table has a unique id field (rough idea)... echo "<td><a href='delete.php?what_id=" . $row['id'] . "'>Delete Me</a>" . $row['links'] . "</td>"; then in delete.php $what_id = $_GET['what_id']; $query = "DELETE FROM sometable WHERE id = '$what_id'"; $result = mysql_query($query); Quote Link to comment https://forums.phpfreaks.com/topic/252854-delete-separate-mysql-records-with-a-button/#findComment-1296375 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.