在这个例子中如何从数据库中删除一行?

时间:2012-05-06 00:59:22

标签: php javascript jquery sql ajax

我要做的是当用户点击“删除”按钮时,它会从服务器中删除该文件,并删除存储相同文件名的数据库行。现在该文件将从服务器中删除成功了,这不是问题。

问题在于它没有删除存储相同文件名的数据库行。如何才能实现此目的?

以下是导航到deleteimage.php的删除按钮的javascript函数:

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

以下是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();

?>

1 个答案:

答案 0 :(得分:2)

看起来你的SQL语法有点偏。试试这个:

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