PHP/MySQL从数据库中删除图像 [英] PHP/MySQL Delete image from database

查看:99
本文介绍了PHP/MySQL从数据库中删除图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个php脚本,可以处理我网站的管理部分/页面的创建.所有数据都保存到名为"isadmin"的数据库表中.在此脚本中,我有一个图像上传表单,该表单将图像添加到单独的数据库"isgallery",然后将其显示回脚本/管理部分.现在,这一切都很好,但是我发现无法删除任何图像.我知道它将删除它们,但是我似乎无法获取要添加到mysql delete调用中的映像的ID.它似乎不存在于while语句之外. (一旦添加了arrrghhh,似乎在识别$ _POST ['imagename']时就出现了问题!).

I have a php script that handles the admin section/creation of pages for my site. All of the data is saved into a database table called 'isadmin'. Within this script I have an image upload form which adds images to a seperate database, 'isgallery' and then displays them back in the script/admin section. Now this all works great, but I'm finding it impossible to then delete any of the images. I know it will delete them, but I can't seem to get the id of the image to be added to mysql delete call. It just doesn't seem to exist outside of the while statement. (There seems to be an issue recognising $_POST['imagename'] once the images are added arrrghhh!).

下面的代码以及任何帮助,我们将不胜感激.

Code below and any help greatly appreciated. S.

这是要删除的代码:

if ($_POST['delGallery']=='1') {        
        $sql = "DELETE FROM isgallery WHERE id = ".mysql_real_escape_string($_POST['isgallery_id']);
        mysql_query($sql);
        //file_exists($galleryFileDir.'/'.$_POST['imagename']) ? unlink($galleryFileDir.'/'.$_POST['imagename']) : NULL;        
        //unset($_POST['imagename']);
    }

这是显示和添加图像的代码:

This is the code to display and add the images:

$galleryQuery=mysql_query("select * from isgallery where assoc_object = '".$_POST['id']."'");
            echo '<ul class="gallery">'. PHP_EOL;            
            while($galleryResult=mysql_fetch_array($galleryQuery)) {    

                echo '<li><img src="../../images/properties/gallery/'.$galleryResult['imagename'].'" width="120" height="120" class="image" /><br />
                      <label for="delGallery"><input type="checkbox" name="delGallery" value="1" /> Delete this image?</label><br />                                                       
                      <p>'.$galleryResult['id'].'</p>
                      <input type="hidden" name="isgallery_id" value="'.$galleryResult['id'].'" />
                      </li>    
                '. PHP_EOL;                        
            }    

            echo '</ul><br /><br />' . PHP_EOL;                                
            echo '<label for="galleryFile">Add Image (*.jpg / *.gif): </label><input type="file" name="galleryFile" value=""><br />
            '.($_POST['imagename'] ? '
            <label for="imagename"></label><img src="../../images/properties/gallery/'.$_POST['imagename'].'" width="120" class="image"><br />                
            <label for="delGallery"></label><input type="checkbox" name="delGallery" value="1" style="margin:0 0 0 7px;"> Delete this image?<br />
            ' : NULL).'    

推荐答案

在显示代码中,将$galleryResult['id']放入您的表单中:

In your display code, place $galleryResult['id'] in your form:

<input type="hidden" name="isgallery_id" value="<?php echo $galleryResult['id'] ?>" />

然后,在您的删除代码中:

Then, in your delete code:

$sql = "DELETE FROM isgallery WHERE id = ".mysql_real_escape_string($_POST['isgallery_id']);

注释:

要删除文件,文件名应存储在isgallery表中.从记录的ID中获取名称.不要依赖用户提供的文件名.

For deleting the file, the file name should be stored in the isgallery table. Grab the name from the record's id. Don't rely on a user supplied file name.

为清楚起见,我的示例代码几乎没有执行数据验证/清理.确保验证并转义要插入数据库的数据.

For clarity, my example code performs little or no data validation/sanitizing. Be sure to verify and escape data being inserted into your database.

这篇关于PHP/MySQL从数据库中删除图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆