Click to See Complete Forum and Search --> : [RESOLVED] Problem with selecting and deleting multiple database entries


roondog
03-12-2008, 08:55 AM
I am working on a gallery that allows a user to upload and delete pictures. So far I have the uploading working fine but have had various problems with deleting. My idea is to have a form with all the pictures showing and each one having a check box. The problem i've had is getting more than one picture to delete. Is there a special way to handle checkboxes. I can't find my code at the moment ( I might have deleted it in frustration) but will post it when I do find it/ re-do it.

sanchez_1960
03-12-2008, 09:32 AM
Use input boxes to post the id numbers of the each image. The input boxes can be can be named id, and then echo the $i of your loop.

Then on the POST loop through the values and execute the query for every id.

roondog
03-12-2008, 10:26 AM
this is my code


<?php
$con = mysql_connect("localhost","******","*****");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("******", $con);

$picture = $POST_[delete_picture];

mysql_query("DELETE FROM delete WHERE img_name = $picture");


mysql_close($con);
?>


and for the form


<html>
<body>
<form action='delete.php' method='post'>
<?php
$con = mysql_connect("localhost","******","******");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("*******", $con);

$result = mysql_query("SELECT * FROM `delete`");

while($row = mysql_fetch_array($result))
{
echo "<img src='".$row['img_name']."' alt='image' />";
echo "<input type='checkbox' name='delete_picture[]' value='".$row['img_name']."'>";
}
mysql_close($con);
?>
<input type='submit'>
</form>
</body>
</html>

ss1289
03-12-2008, 12:16 PM
$picture = $POST_[delete_picture];

foreach($picture as $name) {
mysql_query("DELETE FROM delete WHERE img_name = $name");
}

This might work.

roondog
03-12-2008, 02:22 PM
Not sure whats happening. I've added

echo $name."<br />";

which shows the image names but it's not deleting. I'll have to do more investigating

roondog
03-12-2008, 02:37 PM
got it working just a little syntax error.