Jump to content

Adding a file delete to script


oraya

Recommended Posts

Below is the delete file for our Brochure deletes via a delete button on the update page.  The upload form uploads the file to the "downloads" folder, and also puts the following into the brochures table.  id, title, description and also the full file name example flowers.pdf (which is used for the links).

 

What I want to be able to do on the delete script below, is to not only delete the item if id matches from the table, but also delete the pdf file from the downloads folder before going to the success page via the header.  I tried to add unlink to the script but it didn't work, I probably wasn't doing it correctly.  Hence why I am here.  Is there anyway someone could look at it and tell me how to add this feature to my delete script?

 

Ok hope I've explained this ok, many thanks in advance for any help given.

Oraya

 

<?php

$host = "localhost"; 
$user = "myusername"; 
$pass = "mypassword";
$database = "mydatabasename";

$conn = mysql_connect($host, $user, $pass) or die( mysql_error() );
        mysql_select_db($database) or die( mysql_error() );


$table = mysql_real_escape_string( $_GET["table"] );
$id = mysql_real_escape_string( $_GET["id"] );
$message = mysql_real_escape_string( $_GET["message"] );
$redirect = mysql_real_escape_string( $_GET['redirect'] );
$file = mysql_real_escape_string( $_GET['file'] );



$sql="DELETE FROM $table WHERE id='$id'";


$result = mysql_query( $sql ) or die( mysql_error() );

if($result) {
    header("Location: ../successful.php?message=$message&redirect=$redirect");
}else{
     echo"there was a problem!";
}
mysql_close($conn);

?>

Link to comment
Share on other sites

here

 

<?php

$host = "localhost"; 
$user = "myusername"; 
$pass = "mypassword";
$database = "mydatabasename";

$conn = mysql_connect($host, $user, $pass) or die( mysql_error() );
        mysql_select_db($database) or die( mysql_error() );


$table = mysql_real_escape_string( $_GET["table"] );
$id = mysql_real_escape_string( $_GET["id"] );
$message = mysql_real_escape_string( $_GET["message"] );
$redirect = mysql_real_escape_string( $_GET['redirect'] );
$file = mysql_real_escape_string( $_GET['file'] );

unset("path/to/download/folder/".$file);

$sql="DELETE FROM $table WHERE id='$id'";


$result = mysql_query( $sql ) or die( mysql_error() );

if($result) {
    header("Location: ../successful.php?message=$message&redirect=$redirect");
}else{
     echo"there was a problem!";
}
mysql_close($conn);

?>

Link to comment
Share on other sites

Nope still not deleting it sigh...  could it be because I'm trying to do it on my laptop testing area?  And it's a case that it will work once uploaded to the host?  Though I would have thought it would still work as I have the latest php, mysql etc etc installed.  I'm kinda new to it all so forgive all the questions.

Link to comment
Share on other sites

is the downloads folder in the same folder as the php file

 

is so replace the code with:

 

<?php

$host = "localhost"; 
$user = "myusername"; 
$pass = "mypassword";
$database = "mydatabasename";

$conn = mysql_connect($host, $user, $pass) or die( mysql_error() );
        mysql_select_db($database) or die( mysql_error() );


$table = mysql_real_escape_string( $_GET["table"] );
$id = mysql_real_escape_string( $_GET["id"] );
$message = mysql_real_escape_string( $_GET["message"] );
$redirect = mysql_real_escape_string( $_GET['redirect'] );
$file = mysql_real_escape_string( $_GET['file'] );

unlink("./downloads/".$file);

$sql="DELETE FROM $table WHERE id='$id'";


$result = mysql_query( $sql ) or die( mysql_error() );

if($result) {
    header("Location: ../successful.php?message=$message&redirect=$redirect");
}else{
     echo"there was a problem!";
}
mysql_close($conn);

?>

Link to comment
Share on other sites

no the delete script is in a folder called includes so that I can keep the database login details secure hence the ../  do I need it in the same folder?  If so I can use an include for the log in details.

Link to comment
Share on other sites

I've already done that, it didn't help.  Any other ideas?  I'm so grateful for your help by the way.  I've been googling this for five hours trying to sort it myself before coming here.

Link to comment
Share on other sites

Ok just tried that, that didn't work either sigh...  right I'm going to try uploading the script and see if it works on my web host will be back in a little while.  Thank you so much for this help.

Link to comment
Share on other sites

Will you marry me?  lol...  I tried uploading it and testing it on the host, still didn't work then came back and was about to send you a message when I noticed your comment about echoing the file.  Bingo... I hadn't sent the filename in the links sigh...  Thank you thank you thank you....  I would still be here this time tomorrow if it wasn't for you!  And thank you sooooooooooo much for your patience!!!  Huge kiss for ya..  I'm so very very grateful...

Link to comment
Share on other sites

Will you marry me?  lol...  I tried uploading it and testing it on the host, still didn't work then came back and was about to send you a message when I noticed your comment about echoing the file.  Bingo... I hadn't sent the filename in the links sigh...  Thank you thank you thank you....  I would still be here this time tomorrow if it wasn't for you!  And thank you sooooooooooo much for your patience!!!  Huge kiss for ya..  I'm so very very grateful...

 

if you are a female i will lol ;) glad i could help.

Link to comment
Share on other sites

I am lol...  Oh seriously I've been trying to sort this since very early this evening using google..  it was starting to drive me insane :)  You really have no idea just how happy you've made me.  THANK YOU!!!  One last question where is the SOLVED button?  lol..  Can't seem to see it.

Link to comment
Share on other sites

Good lol nice to meet a girl whos interested in php coding lol whats your site will take a look and if you need anymore help im around.

 

p.s i think you need to edit your first post to change status to solved.. :) David.

Link to comment
Share on other sites

It's for a friend lol..  A funeral directors haha...  I know radical!!  I haven't got it up on a web hosting site as such, well until they register for one later in the week. So just for the testing of that script I signed up for a free host to test it lol..  And I've only uploaded the management area at the moment.  I'll inbox you the proper link at a later date if that's ok!  Once again thank you soooo very much.  :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.