dsnrixf6765 2013-12-15 09:06
浏览 45
已采纳

从图库中删除图像和复制

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">';
?>
  • 写回答

3条回答 默认 最新

  • doufu6130 2013-12-16 06:52
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题