When the user selects to delete a department or location, then the system should check against the database to see if that entry is used in other tables (ie personnel assigned to that department) and, if so, should prevent removal.
What's the best way to do it?
See my code:
<div class="col d-flex justify-content-center">
<div class="btn btn-xs btn-primary5" id="confirmButton" style="display: inline; float: right;" onclick="toggleConfirmDep('Remove', 'removeDepartment()')"></div>
</div>
function removeDepartment() {
let departmentName = $('#removeDepartmentDepartment').val()
$.getJSON(`php/getAllDepartments.php`, function (departments) {
let departmentID = departments.data.filter(dep => dep.name == departmentName)[0].id
$.ajax({
data: {
'id': departmentID
},
url: 'php/deleteDepartmentByID.php',
dataType: 'json',
success: function(data) {
$('#removeDepartmentDepartment').find('option:eq(0)').prop('selected', true);
}
})
});
}
function toggleConfirmDep(message, func) {
if (!event) var event = window.event;
event.cancelBubble = true;
if (event.stopPropagation) event.stopPropagation();
employeeID = $(event.target).closest(".buttons").siblings("form").find("#id").text();
if ($('#confirmDep').css('display') == "none") {
$('#confirmDep').show()
$('#depQuestion').show()
$('#depResponse').hide()
let addOrRemove = capitalizeFirstLetter(message.split(" ")[0])
addOrRemove == "Add" ? addOrRemove += "ed" : addOrRemove += "d"
$('#depMessage').text(message)
$('#depButton').attr('onClick', `
${func.toString()};
$('#depQuestion').hide()
$('#depResponse').show()
$('#depResponseMessage').text('${addOrRemove}')
setTimeout(function(){
$('#confirmDep').hide();
$('#profilePage').hide();
window. location. reload(1);
}, 1500);
`)
} else {
$('#confirmDep').css('display', 'none')
}
}
<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL);
$executionStartTime = microtime(true);
include("config.php");
header('Content-Type: application/json; charset=UTF-8');
$conn = new mysqli($cd_host, $cd_user, $cd_password, $cd_dbname, $cd_port, $cd_socket);
if (mysqli_connect_errno()) {
$output['status']['code'] = "300";
$output['status']['name'] = "failure";
$output['status']['description'] = "database unavailable";
$output['status']['returnedIn'] = (microtime(true) - $executionStartTime) / 1000 . " ms";
$output['data'] = [];
mysqli_close($conn);
echo json_encode($output);
exit;
}
$query = 'DELETE FROM department WHERE id = ' . $_REQUEST['id'];
$result = $conn->query($query);
if (!$result) {
$output['status']['code'] = "400";
$output['status']['name'] = "executed";
$output['status']['description'] = "query failed";
$output['data'] = [];
mysqli_close($conn);
echo json_encode($output);
exit;
}
$output['status']['code'] = "200";
$output['status']['name'] = "ok";
$output['status']['description'] = "success";
$output['status']['returnedIn'] = (microtime(true) - $executionStartTime) / 1000 . " ms";
$output['data'] = [];
mysqli_close($conn);
echo json_encode($output);
?>
What I have tried:
I have tried the seal object assigned object database javascript but it does not work.