0

I'm having trouble deleting images from a mysql database using php. The addition of images to the database works great, and it will display/retieve the images no problem, but it won't delete them properly. It doesn't delete the selected image, always the first one in the database. I've tested it using specific filenames and id's so I know it works, it just doesn't seem to target the selected image.

The gallery table consists of: id, imagename, assoc_table(category) & assoc_object(page-image-is-attached-to).

Hope this makes sense and many thanks in advance.

This is the code used to add and display the images:

$galleryQuery=mysql_query("select * from isgallery where assoc_object = '".$_POST['id']."'");
            echo '<ul class="gallery">'. PHP_EOL;            
            while($galleryResult=mysql_fetch_array($galleryQuery)) {                
                echo '<li><img src="../../images/properties/gallery/'.$galleryResult['imagename'].'" alt="'.$galleryResult['id'].'" width="120" height="120" class="image" /><br />
                      <label for="delGallery"><input type="checkbox" name="delGallery" value="1" /> Delete this image?</label><br />    
                      </li>
                '. PHP_EOL;                            
            }        
            echo '</ul><br /><br />' . PHP_EOL;                                
            echo '<label for="galleryFile">Add Image (*.jpg / *.gif): </label><input type="file" name="galleryFile" value=""><br />
            '.($_POST['imagename'] ? '
            <label for="imagename"></label><img src="../../images/properties/gallery/'.$_POST['imagename'].'" width="120" class="image"><br />                
            <label for="delGallery"></label><input type="checkbox" name="delGallery" value="1" style="margin:0 0 0 7px;"> Delete this image?<br />
            ' : NULL).'    

This is the code that is used to delete from the database:

if ($_POST['delGallery']=='1') {
         file_exists($galleryFileDir.'/'.$_POST['imagename']) ? unlink($galleryFileDir.'/'.$_POST['imagename']) : NULL;                 
         unset($_POST['imagename']);                  
            $sql = "DELETE FROM isgallery WHERE imagename = '".$_POST['imagename']."'";
            mysql_query($sql);                
    } 
5
  • 1
    you need to prevent sql injection in your code Commented Sep 20, 2010 at 14:58
  • 2
    Side note: using a database to store images is not a very handy solution.. Why don't you save the image on your server and only store the url in the database? Commented Sep 20, 2010 at 14:59
  • 1
    +1 for Evert. This is a bad idea to store files in a database, store the path there. Commented Sep 20, 2010 at 15:01
  • @Evert and @Chris both take a deep look at the code. hints: $galleryResult['imagename'] and file_exists($galleryFileDir.'/'.$_POST['imagename']) ? unlink($galleryFileDir.'/'.$_POST['imagename']) : NULL; Commented Sep 20, 2010 at 15:09
  • Apologies, should have stated that only the path is stored in the database, the images are stored on the server. Commented Sep 20, 2010 at 15:09

4 Answers 4

1

the problem is your unset :

 unset($_POST['imagename']); 

before you run the query.

another important thing : prevent from sql injection use mysql_real_escape_string

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

Comments

0

move

unset($_POST['imagename']);  

after $sql in the second example. You are deleting $_POST['imagename'] and trying to use it afterwards.

Comments

0

Something not right here:

unset($_POST['imagename']);                  
$sql = "DELETE FROM isgallery WHERE imagename = '".$_POST['imagename']."'";

Comments

0

Please check the user you are using have delete permissions granted on that table before,

Hope that helps

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.