Page 1 of 1

delete records

Posted: Thu Apr 08, 2004 8:24 am
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

Posted: Thu Apr 08, 2004 10:33 am
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.

Posted: Thu Apr 08, 2004 11:17 am
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.

Posted: Thu Apr 08, 2004 3:08 pm
by RuffRyder
ok, thx guys
imma try that for now ;)

grtz
RuffRyder