Page 1 of 1

delete row from table

Posted: Fri Apr 09, 2004 3:21 am
by RuffRyder
hi all,
got a little problem again. i'm creating an online shop. when users press the "add product" button, a table is shown with the products they ordered, a shopping cart if u will :) next to each row i've placed a delete button so they can delete the products they don't want. when the press the delete button, always the first record in my db table is deleted, not the record they want to delete.

Code: Select all

//showorder.php
global $conn;
$bestelling = "select * from bestdetl where bnummer = 1";
$result = mysql_query($bestelling,$conn);
print "<form name='form1' method='post' action='dealers.php?p=delete'>";
print "<table width='75%' align='center' cellspacing='0' cellpadding='0' border='1'>\n";
while ($row = mysql_fetch_array($result)){
	print "\t<tr onmouseover="selected(this)" onmouseout="deselected(this)" >";
	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> <input type='image' src='images/delete.gif' name='$rownr'> </b></font></td>";
	print "\t</tr>";
}
print "</table>\n";
print "</form>";
mysql_close($conn);

//delete.php
<?
include("database.php");  
global $conn;

$delete = mysql_query('DELETE '.$_POST['$rownr'].' FROM bestdetl LIMIT 1',$conn);
echo "product deleted";
?>
Anyone got an idea when pressing a delete button how to delete the corresponding record?

thx
RuffRyder

Posted: Fri Apr 09, 2004 6:53 am
by magicrobotmonkey
I think the query should be like

DELETE
FROM bestdetl
WHERE RowNo=$_POST['$rownr']
LIMIT 1

Posted: Fri Apr 09, 2004 6:59 am
by markl999
You might need to change :
<input type='image' src='images/delete.gif' name='$rownr'>

to:
<input type='image' src='images/delete.gif' name='delrow' value='$rownr'>

otherwise you won't know which image/button was pressed, then you can do "DELETE FROM bestdetl WHERE whatever={$_POST['delrow']}"

I also don't see where you define/set $rownr in the form, maybe that should be $row['rownr'] ? *shrug* :o