Question

I have a gallery that I'm able to upload images with a title and a short description about the image. I store the images in a folder on my ftp and the data in a database. Here is a screen shot of the database.

enter image description here

I want to give my client a little more control over the gallery by allowing them to update the gallery and delete posts in the gallery. Right now I want to focus on the DELETING part.

I'm using the following code to try and delete the images/post by trying to select by id and delete.

When executing the delete script on the site I get no errors on the page or on my ftp, but the image does not delete.

The end result I'm looking for would be to have the row deleted from the table and the image deleted from the ftp.

I'm very new to php and know I need to learn much more about it, but if someone could help out I would appreciate it. I apologize for the code dump, but not sure how to ask the question without showing what I'm working with.

DELETE CODE:

<?php
//including the database connection file
include($_SERVER['DOCUMENT_ROOT'] . "/connections/dbconnect.php");

//getting id of the data from url
$id = isset($_GET['id']) && $_GET['id'] == $row['id'];

//deleting the row from table
$result=mysql_query("DELETE FROM images where id='$id' limit 1;");   

//redirecting to the display page (index.php in our case)
echo '<table align="center" width="100%" height="100%" border="0"><tr align="center" valign="center"><td><h2>Deleting Image</h2></td></tr></table>';
echo '<meta http-equiv="refresh" content="5;URL=/admin/modify-gallery.php">';
?>

This is the code I'm using to to access the image on the modify-gallery page

modify-gallery code:

include($_SERVER['DOCUMENT_ROOT'] . "/connections/dbconnect.php");
/* be safe, not sorry */
foreach ($_REQUEST as $k => $v) {
    $_REQUEST[$k] = mysql_real_escape_string($v);
}
/* take cat from url if exists */
$category = @$_REQUEST["category"] ? $_REQUEST["category"] : null;
$images = mysql_query(
    $category ?
        sprintf(
            "SELECT * FROM images WHERE data_type = '%s'",
            $category
        ) :
        "SELECT * FROM images"
);
if ($images) {
    $total = mysql_num_rows($images);
    if ($total) {
        $per = 12;
        $page = @$_REQUEST["page"] ? $_REQUEST["page"] : 1;
        $pages = ceil($total/$per);
    }
    mysql_free_result($images);
}
?>

and then this is used to display the images/posts and lists the delete and update button..(same page)

<div class="row">

<ul id="stage" class="portfolio-4column">
<?php
if ($category) {
    $images = mysql_query(sprintf(
        "SELECT * FROM images WHERE data_type = '%s' ORDER BY id DESC LIMIT %d, %d",
        $category, ($page - 1) * $per, $per
    ));
} else $images = mysql_query(sprintf(
    "SELECT * FROM images ORDER BY id DESC LIMIT %d, %d",
    ($page - 1) * $per, $per
));

while ($image=mysql_fetch_array($images))
{
    ?>
<li data-id="id-<?=$image["id"] ?>" data-type="<?=$image["data_type"] ?>">
<div class="grid_3 gallerybox-admin">
<div class="overallheight-admin">
<div class="gallerybox-admin"><a class="fancybox" rel="<?=$image["data_type"] ?>" href="http://<?php echo $_SERVER['SERVER_NAME']; ?>/images/gallery/<?=$image["file_name"] ?>" title="<?=$image["title"] ?>">
<img src="http://<?php echo $_SERVER['SERVER_NAME']; ?>/images/gallery/<?=$image["file_name"] ?>" alt="<?=$image["title"] ?>" class="max-img-border"></a></div>
<div class="galleryh"><?=$image["title"] ?></div>
<div class="galleryp"><?=$image["description"] ?></div>

</div>

<div class="grid_1"><h4 class="btn-green"><a href="http://<?php echo $_SERVER['SERVER_NAME']; ?>/admin/delete.php">Delete</a></h4></div>
<div class="grid_1"><h4 class="btn-green"><a href="#">Update</a></h4></div>

</div>

</li>
    <?php
}
?>
</ul>
</div>

Code from Stack Overflow (Currently Using):

<?php
//including the database connection file
include($_SERVER['DOCUMENT_ROOT'] . "/connections/dbconnect.php");

//getting id of the data from url
$id = isset($_GET['id']) && $_GET['id'] == $row['id'];

//Select image_name(if not known)
$img = mysql_query("Select file_name from images where id=\"$id\"");
$img_res = mysql_fetch_array($img);
$filename = $img_res[0];

unlink($_SERVER['DOCUMENT_ROOT'] . "/images/gallery/" . $filename);
//deleting the row from table
$result=mysql_query("DELETE FROM images where id=\"$id\" limit 1;");  



//redirecting to the display page
echo '<table align="center" width="100%" height="100%" border="0"><tr align="center" valign="center"><td><h2>Deleting Image</h2></td></tr></table>';
echo '<meta http-equiv="refresh" content="5;URL=/admin/modify-gallery.php">';
?>
Was it helpful?

Solution

fix this in delete button html, to pass the file name by the url

<h4 class="btn-green"><a href="admin/remove.php?value=<?=$image["file_name"] ?>">Delete</a></h4></div>

In your remove.php

include("/connections/dbconnect.php");

$filename = isset($_GET['value']) ? $_GET['value'] : NULL;

if (!empty($filename)) {
    $delete = unlink("images/gallery/" . $filename);
    if($delete){
        $result = mysql_query("DELETE FROM images where file_name="'.  mysql_real_escape_string($filename)."' limit 1;")";
        header("Location:succes_page.php");
    }else{
        header("Location:failure_page.php");
    }
}else{
     header("Location:failure_page.php");
}

side note try to update your mysql_* functions to PDO or mysqli

OTHER TIPS

"The end result I'm looking for would be to have the row deleted from the table and the image deleted from the ftp."

the row deleted from the table ✓

But you still need to remove the actual file from your server to do so use unlink($fileName);

//getting id of the data from url
$id = isset($_GET['id']) && $_GET['id'] == $row['id'];

// Delete the file from the server
unlink($_SERVER['DOCUMENT_ROOT'] . "{Path Where Your Images stored}" . $row['file_name']);

//deleting the row from table
$result=mysql_query("DELETE FROM images where id='$id' limit 1;");

As you can see I used the $row['file_name'] to get the file name from you database (good to show us your table structure)

To delete a file from the ftp you should use

unlink(filename with complete path);

Complete Code: //Change Delete code to following

<?php
//including the database connection file
include($_SERVER['DOCUMENT_ROOT'] . "/connections/dbconnect.php");

//getting id of the data from url
$id = isset($_GET['id']) && $_GET['id'] == $row['id'];

//Select image_name(if not known)
$img = mysql_query("Select image_name(your column) from images where id=\"$id\"");
$img_res = mysql_fetch_array($img);
$filename = $img_res[0];

unlink("path to file".$filename);
//deleting the row from table
$result=mysql_query("DELETE FROM images where id=\"$id\" limit 1;");  



//redirecting to the display page (index.php in our case)
echo '<table align="center" width="100%" height="100%" border="0"><tr align="center" valign="center"><td><h2>Deleting Image</h2></td></tr></table>';
echo '<meta http-equiv="refresh" content="5;URL=/admin/modify-gallery.php">';
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top