...

View Full Version : Errors with a delete file script



doyle_loader
06-20-2005, 02:28 PM
Hey everyone.

I'm working at a script that will delete a file from the server and delete the corresponding row in the database. The script is a Online Notes system for a small high school I am working with.

Each row of the script will contain the fields TeacherName, NotesTitle, FileName, CourseName. FileName contains the path of the file that I want to have deleted. When I run my delete script (below) I get an error:


Could not successfully run query (DELETE TeacherName, NotesTitle, FileName, CourseName From Notes WHERE NotesTitle = 'Testing 1, 2, 3') from DB: Not unique table/alias: 'TeacherName'

I'm not exactly sure what's on the go, but here is the script:

<?php
session_register("User");

if (!isset($_SESSION['User']))
{
// User not logged in, redirect to login page
Header("Location: loginsys.php");
}

if ($_SESSION['User'] == "Student")
{
//Students cannot access this section
Header("Location: loginsys.php");
}

/*select note and filename from database*/
$conn = mysql_connect("$host","$username","$password");

if (!$conn) {
echo "Unable to connect to DB: " . mysql_error();
exit;
}

if (!mysql_select_db("dbname")) {
echo "Unable to select the Bay d'Espoir Database: " . mysql_error();
exit;
}

$sql = "Select FileName From Notes";

$result = mysql_query($sql);

if (!$result) {
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
exit;
}

if (mysql_num_rows($result) == 0) {
echo "No rows found, nothing to print so am exiting";
exit;
}
/*done selecting notes and filename*/

//Delete notes!


if (is_file($FileName))
if (@unlink($FileName))
echo "Removed file $FileName";

$sqldel = "DELETE TeacherName, NotesTitle, FileName, CourseName
From Notes
WHERE NotesTitle = '$NotesTitle'";

$resultdel = mysql_query($sqldel);

if (!$resultdel) {
echo "Could not successfully run query ($sqldel) from DB: " . mysql_error();
exit;
}

if (mysql_num_rows($resultdel) == 0) {
echo "No rows found, nothing to print so am exiting";
exit;
}
else
echo "Could not remove file! $FileName";

?>

<html>

<head>

<title>Notes Deleted</title>

<link href="CssFiles/body_frame.css" rel="stylesheet" type="text/css" />


</head>

<body>

<h3><i><? echo $NotesTitle; ?> deleted!</i></h3>

<p>To delete more notes, <a href="delete_notes.php">click here.</a></p>
<p>If you are done<a href="javascript:window.close();">click here.</a></p>

<!-- Make sure content goes before this!!-->
<br> <br>

</body>

</html>

SeeIT Solutions
06-20-2005, 03:01 PM
You should really use an ID field in the table and use the query below to delete a row.


$query = "DELETE FROM `table` WHERE `id` = '$id'";//in your case table = Notes

doyle_loader
06-20-2005, 03:09 PM
Thanks for the quick responce!

I changed my delete statement similar to what you suggested and it deletes the row out of the database fine. However it doesn't delete the file. An example of a FileName value is file_upload/notes/ch1,1[1][1].3structuresincells.doc.



if (is_file($FileName))
if (unlink($FileName))
echo "Removed file $FileName";

$sqldel = "DELETE From Notes
WHERE NotesTitle = '$NotesTitle'";

$resultdel = mysql_query($sqldel);

if (!$resultdel) {
echo "Could not successfully run query ($sqldel) from DB: " . mysql_error();
exit;
}


else
echo "Could not remove file! $FileName";

?>

doyle_loader
06-20-2005, 04:28 PM
I tried echoing out the FileName variable but it appears that the value is not being stored. That's probably the reason why it's not deleting, but I'm not sure why the variable is blank? Any ideas?

Thanks,
Chris

doyle_loader
06-20-2005, 06:52 PM
$sql = "Select * From Notes Where NotesTitle = '$NotesTitle'";
$result = mysql_query($sql);
$data = mysql_fetch_assoc($result);
$FileName = $data['FileName'];

I wasn't putting the field value into a variable so my unlink command wasn't deleting anything!! haha

Cheers,

Chris



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum