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
$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
$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?

First, you only need one form, not one per row.

Second, your forms don’t send any info about the row. Add

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

<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>