Looping to far!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

Looping to far!

Post by Addos »

~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: :arrow: Posting Code in the Forums to learn how to do it too.


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: :arrow: Posting Code in the Forums to learn how to do it too.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Looping to far!

Post by onion2k »

You're using the variable name $file twice. You've used it to hold the value of $_GET['delete'], and then you've used it a second time as the name of the file your call to readdir() returns. Because you're updating the database after you loop through the files in the directory $file will always hold the name of the last file in the directory, and consequently it'll always remove that record from the database rather than removing the one that the user clicked.

I'd do two updates - change the names of your variables to something more verbose and meaningful eg $file_to_be_deleted, and I'd also move the code that generates HTML list of files to the end of the script. It's best to always output things as late as possible in a script.
Post Reply