tags:

views:

53

answers:

4

Hi,

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);                
    } 
A: 

move

unset($_POST['imagename']);  

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

Piotr Pankowski
A: 

Something not right here:

unset($_POST['imagename']);                  
$sql = "DELETE FROM isgallery WHERE imagename = '".$_POST['imagename']."'";
fredley
+1  A: 

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

Haim Evgi
A: 

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

Hope that helps

Ramon Araujo