如何删除上传的图像和图像表记录

时间:2016-09-17 11:03:33

标签: php mysql

从以下代码我可以删除我的上传文件夹图像。但它不会从图像表中删除相关记录。如何完成?我上传的图像保存在upload / 3子文件夹中。

<form method="post" action="delimagehandler.php">
<center>
<input type="submit" value="Delete" name="Delete" >
</center>
<?php
//lets assign the folder name in $title
//You can assign any name
$title= "upload";
/*$directory corresponds to whole path. Edit to your preference. (i assume u store your 
images in directory named "images") */    
$directory = "$title/3";
//The array specifies the format of the files
$allowed_types=array('jpg','jpeg','gif','png');
$file_parts=array();
$ext='';
$title='';
$i=0;

$dir_handle = @opendir($directory) or die("There is an error with your image directory!");

while ($file = readdir($dir_handle)) 
{
    if($file=='.' || $file == '..') continue;

    $file_parts = explode('.',$file);
    $ext = strtolower(array_pop($file_parts));

    $title = implode('.',$file_parts);
    $title = htmlspecialchars($title);

    $nomargin='';

    if(in_array($ext,$allowed_types))
    {
        if(($i+1)%4==0)
        $nomargin='nomargin';
        echo'
        <div id="picture">
        <img src="'.$directory.'/'.$file.'" width="150" height="150"><br>
        Select <input type="checkbox" value="'.$directory.'/'.$file.'" name="imgfile[]">


            </div>';
            echo $file;
            }
            $i++;
        }

    closedir($dir_handle);
    //Now we have the list of images within form elements
    ?>
    </form>

这是delimagehandler.php文件

<?php


$file = $_REQUEST['imgfile'];

$num_files=count($file);
for($i=0;$i<$num_files;$i++)
{
unlink ("$file[$i]");
}

echo "Images successfully deleted.Go <a href='delimage.php'>back</a>";


include("connect.php");
if(isset($_POST['Delete'])){
                if(isset($_POST['check'])){
                    foreach ($_POST['check'] as $value){
                        $sql = "DELETE FROM images  WHERE filename = $value"; 
                        echo " succsfully DELETED Promo ID.$value";
                            echo("<BR>");
                        mysql_query($sql) or die (mysql_error());
                    }
                }
            }

?>

1 个答案:

答案 0 :(得分:1)

将删除查询中提到的变量更改为字符串或其他明智的 Mysql将其作为字段名称

 $sql = "DELETE FROM images  WHERE filename = '$value'";