2016-09-17 58 views
-1

从下面的代码我可以删除我的上传文件夹图像。但它不会从图像表中删除相关记录。如何完成?我上传的图像保存在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()); 
        } 
       } 
      } 

?> 
+0

是你得到任何错误? –

+0

我已经把一个echo $文件;在第一个文件中。它显示相关的图像名称。那么是否有任何方法将该文件传递给第2页?然后我可以删除该值的记录。 – din

回答

1

变化,在删除查询字符串或其他变量提到明智MySQL将采取它作为一个字段名

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

这不会发生。值不能删除表记录。请帮忙。它不会去sql部分。 – din