SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Addict
    Join Date
    Sep 2000
    Location
    Ontario, Canada
    Posts
    320
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    delete function error

    I am attemting to create a function that will delete an entry in a db as well as an associated image file. Currently, the image file gets deleted no problem, just the 'delete' query won't work. I've been trying to figure this thing out for hours but I can't seem to find the problem. So if anyone can spot a potential error with my coding, please let me know.

    When echoing the delete query, it looks okay however the deletion never takes place. The $name is being passed correctly.

    here is the function:

    PHP Code:
    function Delete_Photo($name$directory)
        {
        global 
    $config$db;
        
    $r "DELETE FROM home_photo WHERE (home_photo_name = '$name')";
    echo 
    $r;
        if (
    $r)
        {
        
    $delete "rm $config[install_directory]"."$config[home_photo_directory]"."$name";
        
    exec(escapeshellcmd($delete));
        return 
    true;
        }
    elseif (!
    $r) {
    echo 
    "no go";
    }

    and where its called from the main script:

    PHP Code:
    if ($deleteimage != "")
        {
        
    $result Delete_Photo($deleteimage,$config[home_photo_directory]);
        if ( 
    $result )
        print 
    "$deleteimage has been deleted...";
        } 
    Any ideas are greatly appreciated!

    Thanks a ton.
    Jason Dulberg
    Extreme MTB
    http://extreme.nas.net

  2. #2
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to do mysql_query($r) to execute it...

  3. #3
    SitePoint Addict
    Join Date
    Sep 2000
    Location
    Ontario, Canada
    Posts
    320
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Waaa!!! I knew it was something stupid! I suppose too much staring at coding makes the obvious impossible to notice.

    Thanks for your help!
    Jason Dulberg
    Extreme MTB
    http://extreme.nas.net

  4. #4
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As a side note Jason. PHP has a function for deleting files. unlink() You can use this instead of running system comands from PHP.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  5. #5
    SitePoint Addict
    Join Date
    Sep 2000
    Location
    Ontario, Canada
    Posts
    320
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the info Freddy... I'll have to check that out.

    Are there any benefits to using unlink instead of what I'm using?
    Jason Dulberg
    Extreme MTB
    http://extreme.nas.net

  6. #6
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Someone who knows better can correct me, but I would think it would be more secure to use unlink() from php than to exexcute system commands on your server. Especially system commands with variables in them. I am not about to test this theroy, but someone could possibly put something mailicious into one of those variables and run something malicious on your server. By using unlink() you will take that oppurtunity away. Just a thought.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  7. #7
    SitePoint Addict
    Join Date
    Sep 2000
    Location
    Ontario, Canada
    Posts
    320
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK... I'll have to look into that, thanks.

    I'm running into another problem with the delete function now. It seems that it only works if I have only 1 image. The images are stored in a format like

    10_0.jpg
    10_1.jpg
    and so on. (the 1st part refers to $home_id and the 2nd part is the individual id for each $home_id)

    What I have only deletes "10_0.jpg" - so I figured that it needed a loop... but it doesn't seem to work. The loop needs to start at 0 instead of 1 too..

    PHP Code:
    Function Delete_listing_Photos($delete$directory)
        {
        global 
    $config$db;
        
    $result mysql_query("SELECT home_id,home_photo_name FROM home_photo WHERE home_id='$delete'");
        
    $num_rows mysql_num_rows($result);
        if (!
    $result){
            echo 
    $img_id"<p>"$img;
        }
        while (
    $a_row =mysql_fetch_array ($result)) {
            
    $img_id=$a_row[home_id];
            
    $img=$a_row[home_photo_name];
        
            
    $r "DELETE FROM home_photo WHERE (home_id = '$img_id')";
            
    mysql_query($r);
               if (
    $r) {
                while(
    $num_rows){ 
                    echo 
    $img;
                    
    $delete "rm $config[install_directory]"."$config[home_photo_directory]"."$img";
                    
    exec(escapeshellcmd($delete));
                    return 
    true;
                }
            }
            elseif (!
    $r) {
                echo 
    "no go";
            }
        }

    Thanks for any ideas! Once this is working correctly, I'll take out the system commands and put in unlink()

    Thanks again!
    Jason Dulberg
    Extreme MTB
    http://extreme.nas.net

  8. #8
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't really know your db setup or how this all works but I think you might have better results with something like:

    PHP Code:
    Function Delete_listing_Photos($delete$directory) {
        global 
    $config$db;
        
    $result mysql_query("SELECT home_id,home_photo_name FROM home_photo WHERE home_id='$delete'");
        
    $num_rows mysql_num_rows($result);
        if(
    $num_rows) {
            while (
    $a_row =mysql_fetch_array ($result)) {
                
    $img_id=$a_row[home_id];
                
    $img=$a_row[home_photo_name];
                
                
    $r mysql_query("DELETE FROM home_photo WHERE (home_id = '$img_id')");
                if (
    $r) {
                    
    unlink($config[install_directory].$config[home_photo_directory].$img);
                    return 
    true;
                    }
                else {
                    print 
    "No go";
                    }
                }
            }
        else {
            print 
    "Sorry no photos";
            }
        } 
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  9. #9
    SitePoint Addict
    Join Date
    Sep 2000
    Location
    Ontario, Canada
    Posts
    320
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I just created a listing with 2 images:

    10_0.jpg
    10_1.jpg

    I added your function however, only 10_0.jpg was deleted when I ran the function.

    Sorry, here's what the home_photo table looks like:

    CREATE TABLE home_photo (
    id smallint(6) DEFAULT '0' NOT NULL auto_increment,
    agent_id smallint(6) DEFAULT '0' NOT NULL,
    home_id varchar(30) NOT NULL,
    home_photo_name varchar(30) NOT NULL,
    home_photo_caption varchar(30) NOT NULL,
    PRIMARY KEY (id)
    );

    Thanks for your time!
    Jason Dulberg
    Extreme MTB
    http://extreme.nas.net

  10. #10
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay so let me get this straight you need to grab all the records from home_photo where home_id = $delete or in this case 10 then delete both the records from the database and the images from the directory, right? I think maybe the return true inside the loop might have been causing some problems. Because the other way wasn't working I modified things a bit.

    PHP Code:
    Function Delete_listing_Photos($delete$directory) {
        global 
    $config$db;
        
    $result mysql_query("SELECT home_id,home_photo_name FROM home_photo WHERE home_id='$delete'");
        
    $num_rows mysql_num_rows($result);
        if(
    $num_rows) {
            while (
    $a_row =mysql_fetch_array ($result)) {
                
    extract($a_row);
                
    $images[] = $home_photo_name;
                }
            
    $r mysql_query("DELETE FROM home_photo WHERE (home_id = '$img_id')");
            if (
    $r) {
                foreach(
    $images as $key => $val) {
                    
    unlink($config[install_directory].$config[home_photo_directory].$val);
                    }
                }
            else {
                print 
    "No go";
                }
            }
        else {
            print 
    "Sorry no photos";
            }
        } 
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  11. #11
    SitePoint Addict
    Join Date
    Sep 2000
    Location
    Ontario, Canada
    Posts
    320
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sweeeet mother.... It works like a charm!

    Thanks a ton for your help. I understand how you got it working...!
    Jason Dulberg
    Extreme MTB
    http://extreme.nas.net


Bookmarks

Posting Permissions

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