Hey,
Ok my client wants to be able to delete more than one record at a time, and this is no problem. However, they also want to be able to delete records with files also, now this is causing a bit of a problem.
I am using multiple check boxes by the way.
Below is the code I am using for the script(kinda long, but I want you to be able to see everything):
<table width="544" border="0" cellspacing="0" cellpadding="5">
<tr>
<td class="Normal"> <span class="NewsHeading">Choose a file
to edit:</span></td>
</tr>
<?
// get id
$id = $_POST['getid'];
// generate and execute text query
$query = "SELECT * FROM FILES WHERE subid=$id";
$result = mysql_query($query) or die ("Error in query: $query. " .
mysql_error());
// if records present
if (mysql_num_rows($result) > 0)
{
while($row = mysql_fetch_assoc($result))
{
?>
<tr>
<td class="NewsText">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td bgcolor="FED200"><img src="../images/spacer.gif" width="1" height="2"></td>
</tr>
<tr>
<td>
<table width="100%" border="0" cellpadding="5" cellspacing="0" class="NewsText">
<tr bgcolor="#EFEFEF" onmouseover="this.style.backgroundColor='#FFFFFF';" onmouseout="this.style.backgroundColor='#EFEFEF';">
<td width="345" colspan="3" class="NewsHeading"><?php echo $row['title']; ?><br>
<span class="NewsText">Filename: <?php echo $row['filename']; ?></span></td>
<td width="70" align="center" valign="middle" class="NewsText"><a href="meetings-edit.php?id=<? echo $row['fileid']; ?>"><img src="../images/edit.gif" width="70" height="13" border="0" align="texttop"></a>
<br>
Delete
<? echo '<input type="checkbox" name="records_id[]" value="'.$row['fileid'].'">'; ?></td>
</tr>
<tr align="right">
<td colspan="4" align="right" class="Where2"><img src="images/spacer.gif" width="1" height="1"></td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
<? } }else{ ?>
<tr>
<td>No meetings can be found.</td>
</tr>
<? } ?>
<tr>
<td><input name="getid" type="hidden" id="getid" value="<? echo $id; ?>">
<input name="filename" type="hidden" id="getid" value="<? $row['filename']; ?>">
<? echo '<input type="submit" name="submit" value="Delete selected">'; ?></td>
</tr>
</table>
</form>
<?
if ($_POST['submit']) {
if (!isset($_POST['records_id']) || !count($_POST['records_id'])) {
die('No entries selected, please try again.');
}
$sql = 'DELETE FROM FILES WHERE';
foreach ($_POST['records_id'] as $fid) {
$sql .= ' fileid = '.$fid.' OR';
$file = "/home/admin/filelibrary/".$filename;
if (file_exists($file))
{
unlink("$file");
return ("File $filename has been deleted.");
}else{
return ("$filename could not found!");
}
}
$sql = substr($sql, 0, -3);
mysql_query($sql) or
die('Could not delete records. The error was: '.mysql_error());
echo count($_POST['records_id']).' record(s) deleted successfully.';
}
?>
</td>
</tr>
</table>
When I select the records to be deleted and press submit, I get the following error:
Warning: Unlink failed (Is a directory) in /home/admin/file-edit-choose-2.php on line 226
Now link 226 is this:
unlink("$file");
Any ideas?