Jump to content

Why is php deleting by ASC instead of the selected data?


pneudralics

Recommended Posts

Can't figure why php is deleting by ascending instead of the data that has the delete form. If I have ids 1, 2, 3 showing and try to delete 3, php will delete 1 or if I try to delete 2 php will delete 1.

// While there are rows to be fetched...
while ($list = mysql_fetch_assoc($result)) {

// Get date reading fields
$id = $list['id'];	
$title = $list['title'];
$image = $list['image'];
$category = $list['category'];
echo "<div id=\"displaycolumn\">";		
	if ($numcolsprinted == $numcolszero) {
	echo "<table align=\"center\"><tr>";
	$numcolsprinted = 0;
	}
	if ($numcolsprinted == $numcols) {
	echo "</tr><tr>";
	$numcolsprinted = 0;
	}

echo "<td align=\"center\" width=\"250\">
<b>$title</b>
<br />
<img src=\"$websiteurl/backgrounds/$category/$image\" alt=\"$id\" width=\"200\" height=\"200\" style=\"border:solid;border-color:#000000;\" /><br />
<form action=\"backgroundsdelete.php\" name=\"deleteform\" method=\"post\">
<input type=\"submit\" name=\"delete\" value=\"$id\" />
</form>	
</td>\n";


// Bump up row counter
$numcolsprinted++;

} // End while loop
?>
<div align="center">
<?php
// delete database
if (isset($_POST['delete'])) {
$delete = "DELETE FROM background WHERE id = \"$id\" LIMIT 1";
mysql_query("$delete");

//delete image
$deleteimage = "$backgroundsfolder\\$category\\$image";
unlink("$deleteimage");
echo "<font color=\"red\">Background $id deleted.</font>";
}
// delete end
?>
</div>

Remove the double quotes from around the $id and unless you have multiple rows with the same id and you only want to delete one of them, the LIMIT 1 is pointless. Try this -

 

$delete = "DELETE FROM background WHERE id = $id";

 

If that does not work correctly, do a "view source" of your form in the browser and make sure the values are correct.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.