2017-05-28 149 views
0

如果我在刪除記錄後出現ID,則在控制檯中顯示正確的ID。所以,它似乎是正確連接,但實際上並沒有從我的數據庫中刪除記錄。我無法弄清楚,但它可能是明顯的。使用php/ajax記錄沒有刪除

JS

<script type="text/javascript"> 
$(document).ready(function() { 
    $('.table-row').on('click', function (e) { 
     e.preventDefault(); 
     var $otId = $(this).data('ot-id'); 
     swal({ 
      title: 'Are you sure?', 
      text: "You won't be able to undo this!", 
      type: 'warning', 
      showCancelButton: true, 
      confirmButtonColor: '#FB404B', 
      cancelButtonColor: '#d33', 
      confirmButtonText: 'Yes, delete it!', 
      closeOnCancel: false 
     }, function (isConfirm) { 
      if (isConfirm) { 
       $.ajax({ 
        type: 'post', 
        url: 'functions/delete-ot-request.php', 
        data: {otId: $otId}, 
        success: function (result) { 
         // window.location.href = 'delete-ot.php'; 
         console.log($otId); 
        }, 
       }); 
      } else { 
       swal("Cancelled", "Maybe next time then.", "error"); 
      } 
     }); 
    }); 
}); 
</script> 

PHP

if(isset($_POST['otId'])) { 
    $stmt = $link->prepare("DELETE FROM `ot_request` WHERE `id` = ? LIMIT 1"); 
    $stmt->bind_param("i", $_POST['otId']); 
    $stmt->execute(); 
    $stmt->close(); 
} 

這是一個包含ID的HTML表格行:

<tr class="table-row" data-ot-id="{$id}"></tr> 
+0

你能充分展現html代碼!因爲你發佈tr不可能點擊沒有td –

+0

改進的格式 – purvik7373

回答

0

console.log($otId);報告您先前發送的ID值,而不是從服務器端返回的數據。

因此,這個輸出證明只有ajax的過程是成功的,而不是服務器的工作,這很可能變成什麼錯出於某種原因。

所以,你應該尋找的錯誤在服務器端,有發回一些執行跟蹤開始:

if(isset($_POST['otId'])) { 
    try { 
    $stmt = $link->prepare("DELETE FROM `ot_request` WHERE `id` = ? LIMIT 1"); 
    $stmt->bind_param("i", $_POST['otId']); 
    $stmt->execute(); 
    $stmt->close(); 
    echo 'otId "' . $_POST['otId'] . '" processed'; 
    } catch(PDOException $e) { 
    echo $e->getMessage; 
    } 
} else { 
    echo 'No otId found'; 
} 

和修改你的Javascript,所以你輸出此跟蹤:

success: function (result) { 
    console.log(result); 
}