Prepared statment for unlinking photos before deleting

I have a problem with unlinking photos before deleting the id’s in a prepared statement. The selected ids(of photos) comes through $_POST[‘variable’].

The problem is that it only unlinks ONE photo from the photo folder even if I have selected to delete multiple photos. So it means that the loop is probably not correct.

I wonder if someone can see if there is something wrong with the loop that is supposed to loop through the photo- and thumb paths for the selected photo ids



/* Create the prepared statement */

if ($stmt = $con->prepare("SELECT photo_path, thumb_path FROM photos WHERE photos_id = ?")) {

/* Bind our params */

$stmt->bind_param('i', $photos_id);

/* Execute the prepared Statement - loop through ids*/

foreach ($_POST['variable'] as $photos_id) {
    $stmt->execute();
}


/*Get results and loop through seleced photo and thumb paths*/

$stmt->bind_result($photo_path, $thumb_path);

$stmt->store_result();
$numrows = $stmt->num_rows();

if ($numrows){
	
$I = 0;	
	
while($stmt->fetch()){

unlink("../photos/$photo_path");
unlink("../photos/$thumb_path");

$I++;

}
}
	


/* Close the statement */
$stmt->close();
		
			

} else {
/* Error */
printf("Error: %s\n", $con->error);
}

Show us your form?

Actually no… your problem is a case of where your foreach ends.

stmt->execute does not build up a list of results. If you’re going to do this one item at a time, do all your processing on a single entry at a time.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.