Jump to content

Delete separate MySQL records with a button


Interoth

Recommended Posts

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.

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!

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);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.