-1

From following code I can delete my upload folder images. But It doesn't delete the relevant record from the image table.How can it be done?My uploaded images saved in upload/3 subfolder.

<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>

Here is delimagehandler.php file

<?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());
                    }
                }
            }

?>
din
  • 25
  • 1
  • 6
  • Are you getting any error? – Rajkumar R Sep 17 '16 at 11:42
  • I have put a echo $file; in 1st file. it show the relevant image name. So is there any method to pass that $file to 2 nd page? Then I can delete the record for that value. – din Sep 17 '16 at 22:21

1 Answers1

1

Change that variable mentioned in the delete query as string or other wise Mysql will take it as a field name

 $sql = "DELETE FROM images  WHERE filename = '$value'"; 
Rajkumar R
  • 1,097
  • 8
  • 14
  • This not happens. value cannot delete table records. please help. It doesn't go to the sql part. – din Sep 17 '16 at 21:38