Delete Link
Moderator: General Moderators
Delete Link
If i have code to print the results of a query to a table, then how would i insert in another column a link that when you click it, it will delete the record of that row?
of course...
Now i want to add another column to the table, at the end of the row, and for each row there is a link with the text of 'delete', and if you click on it, it deletes that record. so would it be like <a href="stuff in here to delete record">Delete</a>?
Code: Select all
<?php
$dbc = mysql_connect (localhost,root);
mysql_select_db (afterschool);
$query = "SELECT name FROM students";
$result = @mysql_query ($query);
if ($result) {
echo '<table align="center" cellspacing="0" cellpadding="4"><tr><td align="left"><b>Name</b></td></tr>';
$bg = 'ffff99';
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
$bg = ($bg=='ffff99' ? 'ffff33' : 'ffff99');
echo "<tr bgcolor="", $bg, "" ><td align="left">$row[0]</td></tr>\n";
}
echo '</table>';
mysql_free_result ($result);
} else {
echo '<p>There has been an error. Please try again later.</p>';
}
mysql_close();
?>-
kettle_drum
- DevNet Resident
- Posts: 1150
- Joined: Sun Jul 20, 2003 9:25 pm
- Location: West Yorkshire, England
On the link you click you need to identify two things:
1) The fact that you want to delete something.
2) Something to identify what you want to delete - usually an id number.
The link looks like:
You then take these variables from the url and see that they want to delete something, and that something has the id 15. So with this info create a query to delete a record where id = 15.
As tim said make sure you do some validations before you delete the record or others will be able to just delete anything.
1) The fact that you want to delete something.
2) Something to identify what you want to delete - usually an id number.
The link looks like:
Code: Select all
somepage.php?action=delete&id=15As tim said make sure you do some validations before you delete the record or others will be able to just delete anything.
