单击取消按钮后如何删除数据库行

时间:2012-05-15 18:44:44

标签: php javascript jquery ajax dom

我想在用户点击取消按钮时删除包含文件名的数据库行。问题是它没有删除数据库行。我没做错,数据库行没有被取消?

以下是表格的相关代码:

var $fileImage = $("<form action='imageupload.php' method='post' enctype='multipart/form-data' target='upload_target' onsubmit='return imageClickHandler(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'><label>" + 
  "<input type='button' name='imageCancel' class='imageCancel' value='Cancel' /></label>" +
  "<iframe class='upload_target' name='upload_target' src='#' style='width:0;height:0;border:0px;solid;#fff;'></iframe></form>");

下面是startImageUpload()函数,它启动文件上传并存储取消按钮功能:

  function startImageUpload(imageuploadform, imagefilename){
    $('.imagef1_cancel').eq(window.lastUploadImageIndex).find(".imageCancel").on("click", function(event) {
      return stopImageUpload(2);
      });     
    return true;
  }

下面是stopImageUpload()函数,它使用success和result显示取消消息:

function stopImageUpload(success, imagefilename){
  var result = '';
  if (success == 2){
    result = '<span class="imagecemsg"> The file upload was canceled!</span><br/><br/>';
  } else {
    result = '<span class="imageemsg">There was an error during file upload!</span><br/><br/>';
  }         
  return true;   
}

最后下面是imageupload.php脚本,该脚本使用iframe链接到QandATable.php(包含上面代码的脚本),这是假设要删除数据库行的地方:

<?php
  session_start();
  ...//connected to DB
  $result = 0;

  if ($result == 2) {
    $imagecancelsql = "DELETE FROM Image 
                       WHERE ImageFile = 'ImageFiles/".
                       mysql_real_escape_string($_FILES['fileImage']['name'])."'";
    mysql_query($imagecancelsql);
  }
  mysql_close();
?>
<script language="javascript" type="text/javascript">window.top.stopImageUpload(<?php echo $result ? 'true' : 'false'; ?>, '<?php echo $_FILES['fileImage']['name'] ?>');</script>

1 个答案:

答案 0 :(得分:4)

在imageupload.php中声明:

$result = 0;

然后你说:

if ($result == 2)...

$result为0而不是2,因此不会执行查询。