user1373827
user1373827

Reputation: 23

How to delete a row from the database in this example?

What I am trying to do is that when the user clicks on the "Delete" button, it deletes the file from the server and it removes the database row the same file name is stored in. Now the file deletes from the server successfully, that is not the problem.

The problem is that it is not deleting the database row where the same file name is stored in. How can I get it to achieve this?

Below is the javascript function of the delete button which navigates to the deleteimage.php:

var counter = 0;

function stopImageUpload(success, imagefilename){
    counter++;

    $('.listImage').eq(window.lastUploadImageIndex).append('<div>' + htmlEncode(imagefilename) + '<button type="button" class="deletefileimage" image_file_name="' + imagefilename + '">Remove</button><br/><hr/></div>'); 

    var _counter = counter;

    $('.listImage').eq(window.lastUploadImageIndex).find(".deletefileimage").on("click", function(event) {
        var image_file_name = $(this).attr('image_file_name');

        jQuery.ajax("deleteimage.php?imagefilename=" + image_file_name)
            .done(function(data) {
                $(".imagemsg" + _counter).html(data);
            });

        $(this).parent().remove();
    });

    return true;   
}

Below is deleteimage.php:

<?php

$username="xxx";
$password="xxx";
$database="xxx";

mysql_connect('localhost',$username,$password);

mysql_select_db($database) or die( "Unable to select database");

$image_file_name = $_GET["imagefilename"];

echo "$image_file_name was Deleted";
unlink("ImageFiles/$image_file_name");

$imagedeletesql = "DELETE FROM Image (ImageFile) 
    WHERE ('ImageFile = ImageFiles/". mysql_real_escape_string($image_file_name)."')";

mysql_query($imagedeletesql);

mysql_close();

?>

Upvotes: 1

Views: 172

Answers (1)

McGarnagle
McGarnagle

Reputation: 102743

Looks like your SQL syntax is a bit off. Try this:

$imagedeletesql = "DELETE FROM Image 
    WHERE ImageFile = 'ImageFiles/". mysql_real_escape_string($image_file_name)."'";

Upvotes: 2

Related Questions