Hello,
I have what may be an itty-bitty dumb question, but I’ve been at this for two days and I’m kind of stuck. I don’t even know what key words to Google any more. I’m trying to create an option on a gallery page that allows the user to delete an image and thumbnail from their server and delete data from a MySQL database (in this case the MySQL row contains the image name which has been rewritten to an image_id that auto increments and corresponds with the number given to each image/thumbnail combo uploaded to their respective directories).
I found this code which looks perfect:
I have saved it as delete_images.php, updated my directories and swapped $row[‘image_id’] in for $row[‘image_name’].PHP Code:1. $img_dir = 'image_directory_name/';
2. $img_thmb = 'thumbnail_directory_name/';// if you had thumbnails
3.
4. $image_name = $row['image_name'];//assume that this is the image_name field from your database
5.
6. //unlink function return bool so you can use it as conditon
7. if(unlink($img_dir.$image_name) && unlink($img_thmb.$image_name)){
8. //assume that variable $image_id is queried from the database where your image record your about to delete is...
9. $sql = "DELETE FROM table WHERE image_id = '".$image_id."'";
10. $qry = mysql_query($sql);
11. }else{
12. echo 'ERROR: unable to delete image file!';
13. }
Now for my problem; I can’t seem to find anywhere that tells me how to connect this script with a link on the previous page (gallery.php) which is printing all available image_ids into src html tags so the user can view them.
I know my main problem lies withPHP Code:1. $result = mysql_query('SELECT * FROM images') or die(mysql_error());
2.
3. $odd = true;
4. while ($rows = mysql_fetch_array($result)) {
5. echo ($odd == true) ? '<tr class="odd_row">' : '<tr class="even_row">';
6. $odd = !$odd;
7. extract($rows);
8. echo '<td><img src="' . $thumbdir . '/' . $image_id . '.jpg"></td>';
9. echo 'Options[<a title="Delete File" href="javascript:;"
10. onClick="cf=confirm(\'Are you sure you want to delete?\');if (cf)
11. window.location="delete_images.php?action=gallery&image_id=";
12. return false;">DELETE</a>]';
12. }?>
and I just don’t know how to indicate that based on whatever the current id is, I want the delete_images.php page to run the unlink function using that same $image_id. I may have screwed this up somewhere, because php isn’t my strong point, but I would really appreciate any help or even tutorials on how delete images from a server. If I could figure out how the two pages connect, I think I can troubleshoot myself through any problems on the delete_images.php page.PHP Code:delete_images.php?action=gallery&image_id=
Thank you!


Reply With Quote

Bookmarks