Jump to content

Delete Row and associated images from folder


dutchboy

Recommended Posts

Hope someone can help.

I have an upload script which uploads images and creates a thumbnail and puts them into seperate thumbnail/fullimage folders on the server and adds the file name to my sql database.

 

I have built a simple CMS which enables me to edit individual rows on a database and delete rows using this script

 

<?php

include("config.php");


        $id = $_GET['id'];


        $result = mysql_query("DELETE FROM markers WHERE id='$id' ",$connect);


        header( 'Location:genxml.php' ) ;



?>

 

 

The id is obtained by passing the variable through a hyper link on my "database manager" page.

 

This works well and deletes the particular entry from the database but does'nt deal with the images stored on the server.

 

I was hoping to pass more variables through my hyperlink so that i can use the unlink function in the above script but dont know if this would work. Below is an attempt which might clear things up a little.

 

<?php

include("config.php");

	$imagedir = "dbimages/";

	$thumbnaildir = "dbimages/thimages/";

                $id = $_GET['id'];

	$pic1del = "$imagedir" . $_GET['photo1'];

	$thumb1del = "$thumbnaildir" . $_GET['photo1'];

	unlink($pic1del);

	unlink($thumb1del);


        $result = mysql_query("DELETE FROM markers WHERE id='$id' ",$connect);


        header( 'Location:genxml.php' ) ;



?>

 

 

from link

<a href=delete.php?id=$id&photo1=$photo1>Delete</a>

 

 

so as you can see i want to pass my image names to the delete page, add the file path and delete the images as well as the database row...... but i dont know if im going about it the correct way as im a noob to php. (thumbnail is named the same as the original image which is why i call 'photo1' twice, just saved in the different folders!)

 

hope someone can help,

 

thanks in advance.

I understand what you want to do, maybe it would be easier just to store all the info in the database and just get the image id to be deleted from the query string:

<?php
include("config.php");

$query = mysql_query("SELECT * FROM `markers` WHERE `id`='{$_GET['id']}' ",$connect);
$array = mysql_fetch_assoc($query);

if( unlink("dbimages/{$array['image']}") )
{
    echo "Successfully deleted dbimages/{$array['image']}";
} else
{
    die ("Could not delete dbimages/{$array['image']}");
}

if( unlink("dbimages/thimages/{$_GET['photo1']}") )
{
    echo "Successfully deleted dbimages/thimages/{$array['image']}";
} else
{
    die ("Could not delete dbimages/thimages/{$array['image']}");
}

mysql_query("DELETE FROM `markers` WHERE `id`='{$_GET['id']}' ",$connect);

header('Location:genxml.php');

?>

Wow quick response thanks!

 

Please forgive my stupidity could you ellaborate a little more on "maybe it would be easier just to store all the info in the database and just get the image id to be deleted from the query string"

 

do you mean actually upload the pictures into the database?

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.