如何在此示例中删除数据库行?
I have a problem when the user has clicked on the "Cancel" button. When the user clicks on the "Cancel" button, I want it to delete a row in the database by navigating to the cancelimage.php page. But it is not deleting the database row of the file which has been uploaded but then canceled. By looking at the code below, why is it not deleting the row from the database and how can it be coded so that it does?
Below is the startImageUpload() function where it starts uploading and where the cancel button function is featured:
function startImageUpload(imageuploadform){
...
$(".imageCancel").on("click", function(event) {
var image_file_name = $(this).attr('image_file_name');
jQuery.ajax("cancelimage.php" + image_file_name)
.done(function(data) {
$(".imagemsg" + _cancelimagecounter).html(data);
});
return stopImageUpload();
});
return true;
}
Below is the cancelimage,php page where it is suppose to delete a row from the database?
<?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["fileImage"]["name"];
echo "File Upload was Canceled";
$imagecancelsql = "DELETE FROM Image
WHERE ImageFile = 'ImageFiles/". mysql_real_escape_string($image_file_name)."'";
mysql_query($imagecancelsql);
mysql_close();
?>
Below is the php script (imageupload.php) where it inserts a database row:
<?php
session_start();
$username="xxx";
$password="xxx";
$database="xxx";
mysql_connect('localhost',$username,$password);
mysql_select_db($database) or die( "Unable to select database");
$result = 0;
move_uploaded_file($_FILES["fileImage"]["tmp_name"],
"ImageFiles/" . $_FILES["fileImage"]["name"]);
$result = 1;
$imagesql = "INSERT INTO Image (ImageFile)
VALUES ('ImageFiles/".mysql_real_escape_string($_FILES['fileImage']['name'])."')";
mysql_query($imagesql);
mysql_close();
?>
Looking at your code, I'm pretty sure there would be nothing in the $_FILES variable on the call to cancelimage.php
What you could do is pass the name of the image as a parameter to the cancelimage.php script and the use that in your query.