How can I delete image with php?

Hi, all.
I have faced a problem. I am begginer in PHP so I need your help!!
I have backend system where admin can delete images from database, or upload another one. There is the code I did to delete the image, but something is wrong. I have two php documents, DELETE.PHP and DELETEPRO.PHP. Could you,please help with answer what is wrong and may be gove your solution? Thanks in advance!

Deletepro.php

[php]

<?php require_once("includes/connection.php"); $id=$_GET['shopID']; $query = "SELECT imagename FROM shop WHERE ID='$id"; $result = mysql_query($query) or die('Error, query failed'); mysql_query("DELETE FROM shop WHERE ID='$id'"); while ($row = mysql_fetch_array($result)){ $file = "Images/". $row["imagename"]; } unlink ($file); mysql_close($connection); // Redirect to delete.php. header("location:delete.php"); ?>

[/php]

Delete.php
[php]

<?php require_once("includes/connection.php"); ?>

Delete image

<?php $result=mysql_query("SELECT imagename FROM shop ORDER BY shopID ASC"); while($row=mysql_fetch_array($result)){ echo "Id : $row[$id]
"; echo "Image: $row[imagename]

"; echo '

onclick=“return confirm(‘Are you sure you want to delete this image? \nNotice: the image will be completely gone, and will have to be re-uploaded.’);”

<?php echo ' >Delete this image
'; echo "
"; } mysql_close($connection); ?> [/php]

Is it not deleting the image? because what you have should work if the image path is correct.

Do you mean that you are trying to delete multiple images from a list from that profile?
If so, you have set up the delete loop to dump only one file, not all of them.
So, this code:
mysql_query(“DELETE FROM shop WHERE ID=’$id’”);
while ($row = mysql_fetch_array($result)){
$file = “Images/”. $row[“imagename”];
}
unlink ($file);
Should be something like:
mysql_query(“DELETE FROM shop WHERE ID=’$id’”);
while ($row = mysql_fetch_array($result)){
$file = “Images/”. $row[“imagename”];
unlink ($file);
}

Not sure if that is what you are asking help with…

Sponsor our Newsletter | Privacy Policy | Terms of Service