delete records

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
RuffRyder
Forum Newbie
Posts: 11
Joined: Thu Apr 08, 2004 7:00 am
Location: Belgium

delete records

Post by RuffRyder »

hi,
i'm kinda at this whole php thing and i got a little problem here; when visitors press some button, they get to see a table with some info retrieved from my mysql db
like this:

Code: Select all

<?
while ($row = mysql_fetch_array($result)){
	print "\t<tr>";
	print "\t\t<td align='center'><font color='#666666' face='tahoma'><b>" . $row["model"] . "</b></font></td>";
	print "\t\t<td align='center'><font color='#666666' face='tahoma'><b>" . $row["breedte"] . "</b></font></td>";
	print "\t\t<td align='center'><font color='#666666' face='tahoma'><b>" . $row["lengte"] . "</b></font></td>";
	print "\t\t<td align='center'><font color='#666666' face='tahoma'><b>" . $row["type"] . "</b></font></td>";
	print "\t\t<td align='center'><font color='#666666' face='tahoma'><b>" . $row["stab"] . "</b></font></td>";
	print "\t\t<td align='center'><font color='#666666' face='tahoma'><b>" . $row["cover"] . "</b></font></td>";
	print "\t\t<td align='center'><font color='#666666' face='tahoma'><b>" . $row["aantal"] . "</b></font></td>";
	print "\t\t<td align='center'><font color='#666666' face='tahoma'><b> <img src='images/delete.gif'> </b></font></td>";
	print "\t</tr>";
}
?>
this is to show the table. now my problem is, that when they press the delete button next to te row they want to delete, i want the record to be deleted from the db.
how can i do this best?
don't know quite how to start, first off all, how do i know wich row they want to delete, second, how can i make it a pressing button, not a flat image, third, how do i know if they pressed the delete button, ...

can anyone help me?
thx
RuffRyder
~fleece~
Forum Newbie
Posts: 12
Joined: Sun Apr 04, 2004 10:18 pm

Post by ~fleece~ »

To make a button, you have to make a form. You can do something like:

Code: Select all

echo <<<END
  <form action="delete.php" method="post"> //where delete.php is the file which contains the code for deleting an entry
  <input type="submit" name="button" value="Delete"/>
  </form>
END;
For knowing what row they want to delete...I don't know exactly how to do this with a db...coz I've only done it with a flatfile. But basically, each array element has a unique key. I don't know how you return it from a db. Just read the manua for that. Anyway you can set the key to a certain variable and add it to the form like:

Code: Select all

<input type="hidden" name="key" value="$key"/>
And then in delete.php you can make a test that if $key is equal to the key of an array element, it is that element that gets deleted.
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post by Steveo31 »

I would say to have a button next to each, like dis-

Code: Select all

&lt;input type="submit" value="Delete This Row" name="$theRowYouWantToDelete"&gt;
And have a PHP_SELF code on that page, or another, whichever you want, and have something like this:

Code: Select all

$theQuery = mysql_query('DELETE '.$_POST['theRowYouWantToDelete'].' FROM theTable LIMIT 1';
I haven't really done the DELETE operator much in MySQL so..... yea. That's my idea.
User avatar
RuffRyder
Forum Newbie
Posts: 11
Joined: Thu Apr 08, 2004 7:00 am
Location: Belgium

Post by RuffRyder »

ok, thx guys
imma try that for now ;)

grtz
RuffRyder
Post Reply