I have a table that stores image name in the table and the file in a folder on the server. What i want to do is to be able to delete the record from the database and at the same time, delete the corresponding image which has its name in the table record, from the folder on the server. Any help would be greatly appreciated Below is the code:
if ((isset($_POST['hiddenFieldID'])) && ($_POST['hiddenFieldID'] != "")) {
$deleteSQL = sprintf("DELETE FROM photodiary WHERE pdiaryID=%s",
GetSQLValueString($_POST['hiddenFieldID'], "int"));
mysql_select_db($database_connMain, $connMain);
$Result1 = mysql_query($deleteSQL, $connMain) or die(mysql_error());
$deleteGoTo = "adphotodiary.php?user=" . $row_rsadmin['adID'] . "";
if (isset($_SERVER['QUERY_STRING'])) {
$deleteGoTo .= (strpos($deleteGoTo, '?')) ? "&" : "?";
$deleteGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $deleteGoTo));
}
Thanks a lot to all who helped. Sorry i did not update this earlier but i solved the problem. Of course, with everyone’s help and some further reading.
What i did was to first define the target, then got the id of the field to be deleted from the browser request, ran a query to select the field record from the database based on the id, then used the unlink function in php to unlink the image from the target folder initially defined.
After this, then proceeded to delete the record from the table.
Below is the code.