Page 1 of 1

Delete Link

Posted: Sun Oct 24, 2004 11:11 pm
by meander
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?

Posted: Mon Oct 25, 2004 12:38 am
by phice
Can you show any source code, please?

Posted: Mon Oct 25, 2004 12:49 am
by meander
of course...

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

Posted: Mon Oct 25, 2004 2:18 am
by timvw
provide a link to delete.php?primary_key=value... and then in delete.php just delete the record with that primary key (might want to do some validation first)

Posted: Mon Oct 25, 2004 4:54 am
by meander
huh? im sorry, im no good when it comes to php, getting better, but i wouldnt know how to go about doing something like that... sorry.

Posted: Mon Oct 25, 2004 7:04 am
by kettle_drum
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:

Code: Select all

somepage.php?action=delete&id=15
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.