Delete Link

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
meander
Forum Commoner
Posts: 26
Joined: Sun Oct 10, 2004 3:09 pm

Delete Link

Post 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?
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post by phice »

Can you show any source code, please?
Image Image
User avatar
meander
Forum Commoner
Posts: 26
Joined: Sun Oct 10, 2004 3:09 pm

Post 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>?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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)
User avatar
meander
Forum Commoner
Posts: 26
Joined: Sun Oct 10, 2004 3:09 pm

Post 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.
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post 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.
Post Reply