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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.