1

Here is code of uploading image in my localhost/file/img folder and also inserting image path and name in my table.

<?php

if (isset($_POST['submit']))
  {
  $file_id = $_POST['file_id'];
  if (count($_FILES['upload']['name']) > 0)
    {
    for ($i = 0; $i < count($_FILES['upload']['name']); $i++)
      {
      $tmpFilePath = $_FILES['upload']['tmp_name'][$i];
      if ($tmpFilePath != "")
        {
        $shortname = $_FILES['upload']['name'][$i];
        $filePath = "img/" . $_FILES['upload']['name'][$i];
        if (move_uploaded_file($tmpFilePath, $filePath))
          {
          $files[] = $shortname;
          $query = "insert into images(id,img_name) values('$file_id',' $filePath')";
          mysqli_query($con, $query);
          }
        }
      }
    }

  echo "<h1>Uploaded:</h1>";
  if (is_array($files))
    {
    echo "<ul>";
    foreach($files as $file)
      {
      echo "<li>$file</li>";
      }

    echo "</ul>";
    }
  }

?>

Table Images with attribute img_name type is LONGBLOB now its totally working fine but when i am deleting image from database its getting error that image name is not found. here is code of sending image id and image name using a href

<ul>
  <a href="index.php?img_id=<?php echo urlencode($id); ?>&img=<?php echo urlencode($img); ?>" 
    style="color:red; margin-left:18px;" onclick="return confirm('Are you sure you want to delete this?')" >Delete
  </a>
</ul>

now here is code of want i want to delete from my database and also from my localhost folder named img .

<?php

if (isset($_GET['img_id'], $_GET['img']))
  {
  $id = $_GET['img_id'];
  $img = $_GET['img'];
  $query = "delete from images where id='$id' and image='$img'";
  if (mysqli_query($con, $query))
    {
    unlink($img);
    echo '<script language="javascript">';
    echo 'alert("Image Deleted successfully")';
    echo '</script>';
    }
    else
    {
    echo '<script language="javascript">';
    echo 'alert("image does not exist")';
    echo '</script>';
    }
  } 
?>

now showing warning that img/image_name.jpg not found.Help me please .

3
  • try to debug you query by echo and then run this in mysql Commented Feb 15, 2018 at 10:25
  • That's some horribly unindented code Commented Feb 15, 2018 at 10:45
  • If you are deleting the file refence in database maybe you should to delete the file in the server. Otherwise, your server will have junk files that are not being used. Commented Feb 15, 2018 at 11:24

2 Answers 2

2

I think your delete query is wrong

if(isset($_GET['img_id'] , $_GET['img'])){
$id=$_GET['img_id'];
$img=$_GET['img'];
$query="delete from images where id='$id' and image='$id'";
}

$query="delete from images where id='$id' and image='$img'";

In this query you check Id and Image field with same $id variable

Sign up to request clarification or add additional context in comments.

Comments

2

try this :

    <?php
    if (isset($_FILES['image']['name'])) {
      $name = $_FILES['image']['name'];
      $tmpname1 = $_FILES['image']['tmp_name'];
      $exten = explode(".", $_FILES['image']['name']);
      $exten = $exten[1];
      if ($exten != '') {
        $image_name = "img" . time() . "." . $exten;
      }

      move_uploaded_file($tmpname1, FCPATH . 'assets/admin/uploads/' . $image_name);
      $query = "insert into images(id,img_name) values('your_id',' $image_name')";
      mysqli_query($con, $query);
//see your code
/*
$id=$_GET['img_id'];
$img=$_GET['img'];
$query="delete from images where id='$id' and image='$id'";
*/

you pass the same id value for image. you should try this-

$query="delete from images where id='$id' and image='$img'";
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.