[英]How to delete database row if the success = 2 message appears?
更新:
對,這就是我想要的解決方案。 當用戶單擊“取消”按鈕時,它將嘗試查找將要取消的文件的文件名,然后刪除包含該文件名的數據庫行。
問題在於它無法識別文件名,因為它說明imagecancelname=undefined
。 由於這是未定義的,因此無法刪除包含文件名的數據庫行,因為它無法識別文件名。
因此,問題在於,如何獲取imagecancelname
來識別將要取消的文件的名稱,以便可以用來刪除包含該文件名的數據庫行?
下面是當前的表單代碼:
var $fileImage = $("<form action='imageupload.php' method='post' enctype='multipart/form-data' target='upload_target' onsubmit='return startImageUpload(this);' class='imageuploadform' >" +
"Image File: <input name='fileImage' type='file' class='fileImage' /></label><br/><br/><label class='imagelbl'>" +
"<input type='submit' name='submitImageBtn' class='sbtnimage' value='Upload' /></label>" +
"</p><p class='imagef1_cancel' align='center'></p>" +
"<iframe class='upload_target' name='upload_target' src='#' style='width:0;height:0;border:0px;solid;#fff;'></iframe></form>");
以下是startImageUpload函數,其中在文件上傳時顯示取消按鈕,如果用戶單擊取消按鈕,則其中包含取消按鈕功能:
function startImageUpload(imagecancelname){
$('.imagef1_cancel').eq(window.lastUploadImageIndex).html('<div><button type="button" class="imageCancel" image_cancel="' + imagecancelname + '">Cancel</button></div>');
$('.imagef1_cancel').eq(window.lastUploadImageIndex).find(".imageCancel").on("click", function(event) {
var image_cancel = $(this).attr('image_cancel');
var url = 'cancelimage.php';
window.location=url+"?imagecancelname=" + image_cancel;
});
return true;
}
最后,下面是cancelimage.php腳本,在該腳本中,當用戶單擊“取消”按鈕時,它將導航至該腳本,並且此腳本用於通過使用$ GET方法查找文件名來刪除數據庫行。
<?php
... connected to DB
$image_cancel = $_GET["imagecancelname"];
$imagecancelsql = "DELETE FROM Image
WHERE ImageFile = 'ImageFiles/". mysql_real_escape_string($image_cancel)."'";
mysql_query($imagecancelsql);
mysql_close();
?>
在javascript中,您可以添加一個額外的php文件,以從數據庫中刪除一行。
在您的js中添加它(刪除時):
window.location=url+"?"+id;
url
= php文件所在的路徑
id
=您要刪除的行ID
然后添加一個php文件來做數據庫操作
<?php
$imagecancelsql = "DELETE FROM Image
WHERE ImageFile = 'ImageFiles/".
mysql_real_escape_string($_FILES['fileImage']['name'])."'";
mysql_query($imagecancelsql);
id是一個參數,您需要將其作為參數從javascript傳遞。 例如:如果您想刪除名稱為black
的圖像,則在Java語言中傳遞:
var image = 'black';
window.location=url+"?id="+image;
稍后在php文件中,使用以下命令獲取id的值:
$image = $_GET['id'];
然后執行查詢:
$imagecancelsql = "DELETE FROM IMAGE WHERE IMAGEFILE = $image";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.