Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    May 2005
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Errors with a 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:

    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 Code:
    <?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>

  • #2
    Regular Coder
    Join Date
    May 2005
    Posts
    563
    Thanks
    0
    Thanked 3 Times in 3 Posts
    You should really use an ID field in the table and use the query below to delete a row.

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

  • #3
    New Coder
    Join Date
    May 2005
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


    PHP Code:
    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";

    ?> 

  • #4
    New Coder
    Join Date
    May 2005
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #5
    New Coder
    Join Date
    May 2005
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    $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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •