/    Sign up×
Community /Pin to ProfileBookmark

Errors witha delete file script

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:

[QUOTE]

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’

[/QUOTE]

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

[code=php]<?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>[/code]

to post a comment
PHP

10 Comments(s)

Copy linkTweet thisAlerts:
@scragarJun 20.2005 — in SQL you run deletes like so:"DELETE FROM firstTABLE, secondTABLE WHERE condition"
Copy linkTweet thisAlerts:
@doyle_loaderauthorJun 20.2005 — Thanks for the quick responce!

I changed my delete statement 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.

[code=php]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";

?>[/code]
Copy linkTweet thisAlerts:
@scragarJun 20.2005 — does it say "Removed file $FileName" on your page? if not then you may have to doublecheck the filename since PHP doesn't see it as a file.
Copy linkTweet thisAlerts:
@doyle_loaderauthorJun 20.2005 — Hey,

I get: "Could not remove file! $FileName"

Could it have anything to do with having quotations around the file name path?

In the database it is stored without quotes.

Cheers,

Chris
Copy linkTweet thisAlerts:
@scragarJun 20.2005 — doesn't matter(when it's pulled from the DB it's a string, which is all the quotes do), that should work.

can you show me a link or something?
Copy linkTweet thisAlerts:
@doyle_loaderauthorJun 20.2005 — The website is http://www.k12.nf.ca/bda/bay.html. It's under the login section. I'll Private message the Administration password if you need it. Just let me know.

And I have javascript enabled for this website, just in case you may have it turned off in your browser.

Chris
Copy linkTweet thisAlerts:
@scragarJun 20.2005 — Firstly can you send me a few examples of these file links?
Copy linkTweet thisAlerts:
@doyle_loaderauthorJun 20.2005 — Try going to student web > online notes > Biology 2201 and click get notes on the aforementioned website.

That brings you to a simple page where it will display the files for that course with download links.

Is that a good enough example?

Chris
Copy linkTweet thisAlerts:
@doyle_loaderauthorJun 20.2005 — I just tried echoing out the variable to see if their is a value but it seems like the $FileName is not getting a value. Any idea what could be going on here?

Chris
Copy linkTweet thisAlerts:
@doyle_loaderauthorJun 20.2005 — [code=php]
$sql = "Select * From Notes Where NotesTitle = '$NotesTitle'";
$result = mysql_query($sql);
$data = mysql_fetch_assoc($result);
$FileName = $data['FileName']; [/code]

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

Cheers,

Chris
×

Success!

Help @doyle_loader spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...