2016-09-17 56 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