When I'm running the following ajax call, after clicking the button with id removeAttachment, and data attribute "data-recordid", the script executes the PHP script accordingly, but still the server responds with an error code 500. This results in not finishing the ajax call.
JS
$('#removeAttachment').on("click", function(e){
e.preventDefault();
var recordid = $(this).data('recordid');
if(confirm("Weet je zeker dat je deze bijlage wilt verwijderen?")){
$.ajax({
type: 'POST',
url: ('/functions/maintenance_functions.php?action=removeAttachment'),
data: {recordId: recordid},
cache: false,
success: function(data,status) {
$('#currentAttachment').hide();
$('#record_table').bootstrapTable('refresh');
}
})
}
});
PHP - Please note that the php script contains various "actions" which are called by other ajax queries (and are working without a problem!)
if($action=="removeAttachment" && isset($_POST['recordId']) && get_maintequipmentrecord_info($_POST['recordId'],"equipment_id")==$_SESSION['active_maint_equipment'] && can_super_access($page)==true){
$attachment = get_maintequipmentrecord_info($_POST['recordId'],"attachment");
$attachment_file = $_SERVER['DOCUMENT_ROOT']."/_files/maintenance/attachments/".$attachment;
if($attachment!='' && file_exists($attachment_file)) {
unlink($attachment_file);
}
$stmt = $connect->prepare("UPDATE maintenance_records SET attachment=NULL WHERE record_id=?");
$stmt->bind_param("si", $_POST['recordID']);
$stmt->execute();
$stmt->close();
}
Unfortunately i really can't find what the issue is, as the PHP script and attached functions are executed accordingly (the file is removed from the server, and the database field is set to NULL. Anyone a clue? Thanks!
Edit 12/9/17 Updated the JS at the data part. Also updated the PHP code for inserting the data into MySQL, in order to prevent SQL injections. Many thanks for the comments on that! But still, the script returns an error 500. The PHP log shows the following:
[12-Sep-2017 21:04:45 Europe/Berlin] PHP Warning: >mysqli_stmt::bind_param(): Number of elements in type definition string >doesn't match number of bind variables in >/Applications/MAMP/htdocs/functions/maintenance_functions.php on line $ [12-Sep-2017 21:04:45 Europe/Berlin] PHP Stack trace: [12-Sep-2017 21:04:45 Europe/Berlin] PHP 1. {main}() >/Applications/MAMP/htdocs/functions/maintenance_functions.php:0 [12-Sep-2017 21:04:45 Europe/Berlin] PHP 2. mysqli_stmt->bind_param() >/Applications/MAMP/htdocs/functions/maintenance_functions.php:104
Still, the file is deleted from the server, so apparently it can execute some part of the code! But the value is not deleted from the database, but i can't see what's wrong in the SQL query. FYI, the application is running on a local webserver (nginx + mysql), which will always be disconnected from the internet.