I am trying to delete a row off my db as well as a file. I can get the script to delete a row, but the file still remains.

In my db I have a field titled "file" this field stores just the name of the file.
The actual file is stored in a subdirectory called "media"

Now in my admin section I created a list all page (list.php) which allows admins to update or delete a row.

When the admin clicks on the "delete" hyperlink - the page calls for delete.php

The delete.php has this code:

<?php
session_start();
if(!session_is_registered(myusername)){
header("location:login.php");
}

include("includes/config.inc.php");

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// get value of id that sent from address bar *This is from the list.php page
$id=$_GET['id'];
// Delete data in mysql from row that has this id 
$sql="DELETE FROM $tbl_name WHERE id='$id'";
$result=mysql_query($sql);

// if successfully deleted
if($result){
echo "Deleted Successfully";
echo "<BR>";
echo "<a href='list.php'>Back to main page</a>";
}

else {
echo "ERROR";
}

// close connection 
mysql_close();

?>

I know that I am to use the unlink command:

$file = "../media/['file']";
unlink($file); 

But how do I Get the $file value to reflect the value from the field?

    You're going to have to perform a SELECT query before you DELETE the row so that you can retrieve this 'file' column.

      Ok let me see if I have this straight... Let's say "file" is my field name in my table...
      Would my new code be?

      $id=$_GET['id'];
      $sql="SELECT file FROM $tbl_name WHERE id='$id'";
      $result=mysql_query($sql);
      $file = "../media/[$result]";
      unlink($file);
      
      $sql="DELETE FROM $tbl_name WHERE id='$id'";
      $result=mysql_query($sql);
      
      // if successfully deleted
      if($result){
      echo "Deleted Successfully";
      echo "<BR>";
      echo "<a href='list.php'>Back to main page</a>";
      }
      
      else {
      echo "ERROR";
      }
      
      // close connection 
      mysql_close();
        1. $result is a MySQL resource, not a string of text. To actually retrieve data, you must use one of the fuctions that fetches data using the resource returned from mysql_query(). In this case, you could use [man]mysql_result/man since you only want 1 column from 1 row.

        2. There's no reason to add []'s around the variable in the file path... these will simply appear literally in the string, breaking the file path.

          I tried this and still not working;

          $result = mysql_query('SELECT file FROM $tbl_name WHERE id=$id');
          $file = "../media/$result"; 

          The above $file is the path to my subdirectory where the media file resides. The $result value should be the query that we ran in the previous line. So if I select file (which is my field name) FROM $tbl_name (my table) WHERE id=$id (this is from the previous page - this is the id row I am trying to get the value from the file field and plug it into the $result. I can't unlink the file until I assign the file to be unlinked.

          unlink($file);

          What would be the right code?

            This works, but is there an easier way of writing this code?

            <?php
            include("includes/config.inc.php");
            
            // Connect to server and select databse.
            mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
            mysql_select_db("$db_name")or die("cannot select DB");
            
            // get value of id that sent from address bar 
            $id=$_GET['id'];
            
            // First query - unlink media file from subdirectory
            $sql="SELECT * FROM $tbl_name WHERE id='$id'";
            $result=mysql_query($sql);
            $rows=mysql_fetch_array($result);
            $target="../mp3/";
            $file=$rows['file'];
            $path="$target$file";
            unlink($path);
            ?>
            
            

              Ok now I think I have it... After many hours of trial and error my final code is:

              <?php
              session_start();
              if(!session_is_registered(myusername)){
              header("location:login.php");
              }
              include("includes/config.inc.php");
              
              // Connect to server and select databse.
              mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
              mysql_select_db("$db_name")or die("cannot select DB");
              
              // get value of id that is sent from address bar
              $id = intval($_GET['id']);
              
              // First query - fetch file name
              $sql = "SELECT file FROM ".$tbl_name." WHERE id=".$id;
              $result = mysql_query($sql) or die("SQL Error: ".mysql_error());
              
              if (mysql_num_rows($result) > 0) {
                $rows = mysql_fetch_array($result);
              
              // Unlink media file from subdirectory
                $path = "../mp3/".$rows['file'];
                unlink($path);
                echo "Deleted mp3 file Successfully";
              }
              
              // Delete data in mysql from row that has this id 
              $sql="DELETE FROM" .$tbl_name. "WHERE id=".$id;
              $result = mysql_query($sql) or die("SQL Error: ".mysql_error());
              
              // if successfully deleted
              if($result){
              echo "Deleted Successfully";
              echo "<BR>";
              echo "<a href='list.php'>Back to main page</a>";
              }
              
              else {
              echo "ERROR";
              }
              
              // close connection 
              mysql_close();
              ?>
              
                Write a Reply...