Hi,
I have the following code that I’m using to delete a file in a folder and also the name of this file in the database. The problem is that it’s deleting all the file names in the database when I click on anyone of the files to be deleted in the list.
I can see that the code is looping through the whole $updateSQL query too and deleting each name so I’m wondering how to only pass the exact value only that I’m clicking on and in effect stop the loop.
Hope this makes sense.
Code: Select all
$dir = $_SERVER['DOCUMENT_ROOT'] . '/tutti_audio/mp3/';
$file = isset($_GET['delete']) ? basename(urldecode($_GET['delete'])) : false;
if ($file && file_exists($dir . $file)) {
unlink($dir.$file);
}
// Define the full path to the folder whose contents you want to list
$path = "../../tutti_audio/mp3/";
// Open the directory
$dir_handle = @opendir($path) or die("Error opening $path");
// Loop through the files
while ($file = readdir($dir_handle)); {
if($file == "." || $file == ".." || $file == "index.php" ) { continue; }
echo '<a href="?delete='. urlencode($file). '"> ' .$file .'</a><br/>' .' <br>';
}
if(isset($_GET['delete'])) {
$updateSQL = sprintf("UPDATE tbl_products SET audio=null WHERE audio='$file'");
mysql_select_db($database_ar****, $*****);
$Result1 = mysql_query($updateSQL, $*******) or die(mysql_error());
} ?>~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: