SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2012
    Location
    Web
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem deleting the correct files

    I am facing problem deleting the correct files. I am displaying the list of files uploaded by the user sorted by the time of upload (last upload first). If there's a list of 3-4 files, no matter which file I click to delete, the first file in the list gets deleted, the file last uploaded that is. Here is my page displaying the files a particular user has uploaded.
    PHP Code:
    <?php
    $uid
    =$faculty_data['faculty_id']; //Assigns logged in id to a variable
    $query="SELECT * FROM uploads ORDER BY datetime DESC"//Sorts by date time
    $result=mysql_query($query);
    while(
    $row=mysql_fetch_assoc($result))
    {
    if(
    $uid==$row['faculty_id']) //Checks if the logged in id matches with id in DB
    {
    echo 
    '<form action="delete.php" method="POST">';
    echo 
    "<strong>File: </strong>";
    $url=$row['link'];  
    $new="http://tofsis.com/fileshare/".$url
    echo 
    "<a href='$new'>$new</a><br/>";
    echo 
    "<strong>On: </strong>".$row['datetime'];
    echo 
    '<br><input type="submit" name="delete" class="btn btn" value="Delete File"/>';
    echo 
    '<hr>';
    echo 
    '</form>';
    }
    }
    ?>
    And this is my delete page:
    PHP Code:
    <?php
    $uid
    =$faculty_data['faculty_id'];
    $query="SELECT * FROM uploads ORDER BY datetime DESC";
    $result=mysql_query($query);
    if(isset(
    $_POST['delete']))
    {
     while(
    $row=mysql_fetch_assoc($result))
    {
    if(
    $uid==$row['faculty_id'])
    {
     
    $url=$row['link'];
     
    $new="http://tofsis.com/fileshare/".$url
     
    $query="DELETE FROM uploads WHERE link = '$url'";
     
    $result=mysql_query($query);
     
    unlink($url);
    }
    }
     
    header('Location: my_uploads.php');
     exit();
     }
     else {
     echo 
    '<script type="text/javascript">alert("Oops something went wrong!")</script>';
     
    header('Location: my_uploads.php');
     exit();
     }
    ?>
    Can anyone please tell me where I am going wrong so that I can get my problem fixed?

  2. #2
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    First, you only need one form, not one per row.

    Second, your forms don't send any info about the row. Add
    PHP Code:
    var_dump ($_POST); 
    to the top of your delete page to see what the form is sending.

    The file rows could be check boxes. The user would check the files to be deleted
    Code HTML4Strict:
    <form action="delete.php" method="POST">
      <input type="checkbox" name="file[]" value="FileID1">FileID1</input><br>
      <input type="checkbox" name="file[]" value="FileID2">FileID2</input><br>
      <input type="checkbox" name="file[]" value="FileID3">FileID3</input><br>
      <input type="checkbox" name="file[]" value="FileID4">FileID4</input><br>
      <input type="submit" name="action" value="Delete">
    </form>
    Denny Schlesinger
    web services


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •