2017-10-20 255 views
0

我有一個按鈕,它調用這個AJAX請求來刪除記錄。 我怎麼能顯示/手柄MySQL的錯誤,正確地想:通過AJAX請求刪除

Error: Table 'supplier_contacts' doesn't exist

// DELETE 
$('.delete-btn').click(function() { 

    // Confirm 
    if (!confirm('Are you sure want to delete this row?')) { 
    return false; 
    } 

    // id need to delete 
    var contact_id = $(this).attr('contact_id'); 

    // Current button 
    var obj = this; 

    // Delete by ajax request 
    $.ajax({ 
    type: "post", 
    dataType: "text", 
    url: 'suppliers_sql.inc.php?a=delete_contact', 
    data: { 
     contact_id: contact_id 
    }, 
    success: function (result) { 
     $(obj).parent().parent().remove(); 
     window.location.assign('suppliers_details.php?id=<? echo $supplier_id ?>&m=success'); 
    } 
    }); 

}); 


SQL QUERY of suppliers_sql.inc.php ? a = delete_contact 

// -------------------------------------------------------------------------------------------- 
// DELETE CONTACT 
// --------------------------------------------------------------------------------------------  
if ($_REQUEST['a'] == "delete_contact") { 

    $contact_id = $_POST['contact_id']; 

    $sql_contact = "DELETE FROM supplier_contacts WHERE contact_id = $contact_id"; 

    if (mysqli_query($mysqli, $sql_contact)) { 
    mysqli_close($mysqli); 
    //header("Location: suppliers_details.php?id=$supplier_id&m=success"); 
    exit; 
    } else { 
    echo "Error: " .$sql_contact. "<br>" .mysqli_error($mysqli); 
    mysqli_close($mysqli); 
    exit; 
    } 

} 

回答

0

當你正在呼應的東西,如果有,你可以在你的成功顯示它是一個錯誤。

$.ajax({ 
    type : "post", 
    dataType : "text", 
    url : 'suppliers_sql.inc.php?a=delete_contact', 
    data : { 
     contact_id : contact_id 
     }, 
     success : function(result){ 
     if(result) 
     { 
      //Your error 
      alert(result); 
     } 
     else 
     { 
      //all good 
      $(obj).parent().parent().remove(); 
      window.location.assign('suppliers_details.php?id=<? echo $supplier_id ?>&m=success'); 
     } 
     }      
    }); 
+0

非常感謝。完美工作。 –