www.webdeveloper.com
Results 1 to 10 of 10

Thread: Editting a Gallery - How to delete photos

  1. #1
    Join Date
    Mar 2010
    Posts
    15

    Editting a Gallery - How to delete photos

    Hey guys,

    I'm working on a very simple php based backoffice where the admin can add or delete photos. Here's the code for the whole gallery thing:

    Code:
    <div class="bo_content_mid">
         <?php while ($website_row = mysql_fetch_array($website_result)) { ?>
              <div class="content_item">
                   <div><a href="<?php echo $website_row['image']; ?>" class="thickbox"><img src="<?php echo $website_row['thumbnail']; ?>"></a></div>
              <div class="content_item_delete"><a href="#">Delete</a></div>
         </div>
         <?php } ?>
    </div>
    My question is: how do I get the "delete" button to delete the picture? I know it may be quite simple but I'm not being able to get my head around it without deleting everything in the process.

  2. #2
    Join Date
    Mar 2010
    Posts
    2,803
    Quote Originally Posted by impaKt View Post
    Hey guys,
    My question is: how do I get the "delete" button to delete the picture? I know it may be quite simple but I'm not being able to get my head around it without deleting everything in the process.
    It depends om whether the info about the images are stored in a database or not.

    If is, then basically in the href I would send the id of the image to a server side script that deleted the info for the image from the database and then deletes the corresponding physical image file for that image id from from the hard disk.

  3. #3
    Join Date
    Jul 2010
    Location
    Texas
    Posts
    76
    I recently had to delete something from the database and the file as tirna said.

    use unlink to remove the file from the server and use the DELETE mysql command to remove a row.

    something like, as tirna suggested:

    PHP Code:
    //check to see if the file exists first
    if( file exists ){
       
    unlinkimg/file/path );
       
    mysql_query"DELETE FROM tbl_name WHERE image='$image'" );

    Of course, I'm still new so I could be wrong and this is very basic
    Last edited by antares; 08-27-2010 at 02:47 AM.

  4. #4
    Join Date
    Mar 2010
    Posts
    15
    Well first off, thanks for the replies guys!

    And yeah, that's pretty much of what I had tried. My database rows for each item are as follows:

    id, thumbnail, image, category_id and created_at collumns.

    However, when I try selecting one of them using their own id (like tirna said) I always select all of them because they're inside a "while". If I get the "delete" button to delete the row with that specific "id" from the database, what will happen is that it will select every row and delete them all instead of just one.

  5. #5
    Join Date
    Jul 2010
    Location
    Texas
    Posts
    76
    Is the delete script receiving the proper id? How are you calling the delete script? Would this not work?

    Code:
    <div class="content_item_delete"><a href="delete.php?id=<?php echo $website_row[id]; ?>">Delete</a></div>

  6. #6
    Join Date
    Mar 2010
    Posts
    2,803
    Imo a better way to do it is to get the php code to generate a checkbox something like this next to each image:

    Code:
    <input type="checkbox" name="delMe[]" value="imageId" />
    where "imageId" is the image Id for the respective image from the database.

    The user then checks the checkboxes for the images they want to delete and then clicks a "Delete" button.

    When the "Delete" button is clicked an array called delMe[] containing the id's of the images to be deleted is sent to server side script.

    Then the server side script loops through the delMe array of id's and deletes the appropriate records from the database only for those id's in delMe and then deletes the corresponding physical files from disk for those id's.

    This way you can delete multiple images on 1 click of the "Delete" button as opposed to deleting 1 image at a time.

    But which way you do it is your call.
    Last edited by tirna; 08-27-2010 at 03:12 AM.

  7. #7
    Join Date
    Mar 2010
    Posts
    15
    alright, so first of the problem solved with that side script, thanks

    I used the following code:
    Code:
    <?php
    echo("Image number $_GET[id] successfully deleted");
    mysql_query("DELETE from photos WHERE id='$_GET[id]'");
    ?>
    Now I can get to the second part of my question: in the database, for each image, there are 2 links: one for the full sized image and another one for the thumbnail. Is there a way that I can get this delete button to also delete both images from the hard disk they are hosted in?

  8. #8
    Join Date
    Jan 2009
    Posts
    3,346
    To answer that we would need to see what the data schema is for the affected tables (including any constraints).

  9. #9
    Join Date
    Mar 2010
    Posts
    2,803
    Quote Originally Posted by impaKt View Post
    alright, so first of the problem solved with that side script, thanks

    I used the following code:
    Code:
    <?php
    echo("Image number $_GET[id] successfully deleted");
    mysql_query("DELETE from photos WHERE id='$_GET[id]'");
    ?>
    Now I can get to the second part of my question: in the database, for each image, there are 2 links: one for the full sized image and another one for the thumbnail. Is there a way that I can get this delete button to also delete both images from the hard disk they are hosted in?
    As soon as you have confirmed in your script that the image's record in the database has been deleted you can use unlink() as suggested by antares earlier to delete the physical files for that image from the hard disk.

  10. #10
    Join Date
    Mar 2010
    Posts
    15
    That worked Thanks again!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles