connect_error) { die("Connection failed: " . $conn->connect_error); } // Handle Delete Action if (isset($_POST['delete']) && isset($_POST['birth_number'])) { $birth_number = $_POST['birth_number']; $conn->begin_transaction(); try { // Delete from Patient_Diagnoses first (due to foreign key) $stmt = $conn->prepare("DELETE FROM Patient_Diagnoses WHERE birth_number = ?"); $stmt->bind_param("s", $birth_number); $stmt->execute(); // Then delete from Patients $stmt = $conn->prepare("DELETE FROM Patients WHERE birth_number = ?"); $stmt->bind_param("s", $birth_number); $stmt->execute(); $conn->commit(); $message = '
Patient deleted successfully!
'; } catch (Exception $e) { $conn->rollback(); $message = '
Error deleting patient: ' . $e->getMessage() . '
'; } } // Handle Add New Patient if (isset($_POST['add'])) { $birth_number = $_POST['birth_number']; $surname = $_POST['surname']; $first_name = $_POST['first_name']; $diagnosis_code = $_POST['diagnosis_code']; $nurse_id = $_POST['nurse_id']; $medicine_id = $_POST['medicine_id']; if (empty($birth_number) || empty($surname) || empty($first_name) || empty($diagnosis_code) || empty($nurse_id) || empty($medicine_id)) { $message = '
All fields are required!
'; } else { $conn->begin_transaction(); try { $stmt = $conn->prepare("INSERT INTO Patients (birth_number, surname, first_name) VALUES (?, ?, ?)"); $stmt->bind_param("sss", $birth_number, $surname, $first_name); $stmt->execute(); $stmt = $conn->prepare("INSERT INTO Patient_Diagnoses (birth_number, diagnosis_code, nurse_id, medicine_id) VALUES (?, ?, ?, ?)"); $stmt->bind_param("ssii", $birth_number, $diagnosis_code, $nurse_id, $medicine_id); $stmt->execute(); $conn->commit(); $message = '
Patient added successfully!
'; } catch (Exception $e) { $conn->rollback(); $message = '
Error: ' . $e->getMessage() . '
'; } } } // Get data for dropdowns $diagnoses_query = "SELECT diagnosis_code, diagnosis_name FROM Diagnoses"; $nurses_query = "SELECT nurse_id, nurse_name FROM Nurses"; $medicines_query = "SELECT medicine_id, medicine_name FROM Medicines"; $diagnoses = $conn->query($diagnoses_query); $nurses = $conn->query($nurses_query); $medicines = $conn->query($medicines_query); // Get all patient data $sql = "SELECT p.birth_number, p.surname, p.first_name, d.diagnosis_code, d.diagnosis_name, n.nurse_name, m.medicine_name FROM Patients p JOIN Patient_Diagnoses pd ON p.birth_number = pd.birth_number JOIN Diagnoses d ON pd.diagnosis_code = d.diagnosis_code JOIN Nurses n ON pd.nurse_id = n.nurse_id JOIN Medicines m ON pd.medicine_id = m.medicine_id ORDER BY p.surname, p.first_name"; $result = $conn->query($sql); ?> Patient Management System

Patient Management System

Add New Patient

Current Patients

num_rows > 0): ?> fetch_assoc()): ?>
Birth Number Surname First Name Diagnosis Code Diagnosis Name Attending Nurse Prescribed Medicine Action
">
No records found
close(); ?>