delete row from table

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 row from table

Post 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
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

I think the query should be like

DELETE
FROM bestdetl
WHERE RowNo=$_POST['$rownr']
LIMIT 1
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

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