How to develop Projects(HMS) Using PHP and Mysql
Part 10
appointment
add_appintment.php
<?php
include '../../db/db.php'; // Database connection
$error = "";
// Fetch doctors for dropdown
$doctors = $conn->query("SELECT id, name FROM doctors");
// Handle form submission
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['add_appointment'])) {
$doctor_id = intval($_POST['doctor_id']);
$patient_name = $conn->real_escape_string($_POST['patient_name']);
$appointment_date = $_POST['appointment_date'];
$appointment_time = $_POST['appointment_time'];
// Validate inputs
if (empty($doctor_id) || empty($patient_name) || empty($appointment_date) || empty($appointment_time)) {
$error = "All fields are required!";
} else {
// Insert into database
$sql = "INSERT INTO appointments (doctor_id, patient_name, appointment_date, appointment_time)
VALUES ('$doctor_id', '$patient_name', '$appointment_date', '$appointment_time')";
if ($conn->query($sql) === TRUE) {
echo "<script>alert('Appointment added successfully!'); window.location='view_appointments.php';</script>";
exit;
} else {
$error = "Error: " . $conn->error;
}
}
}
?>
<!-- 🏥 Add Appointment Form -->
<div class="content">
<div class="container mt-4">
<h2 class="text-center text-primary">📅 Schedule New Appointment</h2>
<?php if (!empty($error)): ?>
<div class="alert alert-danger"><?= $error; ?></div>
<?php endif; ?>
<div class="card shadow-lg p-4">
<form action="" method="post">
<div class="mb-3">
<label class="form-label"><strong>Doctor:</strong></label>
<select name="doctor_id" class="form-select" required>
<option value="">-- Select Doctor --</option>
<?php while ($doctor = $doctors->fetch_assoc()): ?>
<option value="<?= $doctor['id']; ?>"><?= htmlspecialchars($doctor['name']); ?></option>
<?php endwhile; ?>
</select>
</div>
<div class="mb-3">
<label class="form-label"><strong>Patient Name:</strong></label>
<input type="text" name="patient_name" class="form-control" required placeholder="Enter patient's name">
</div>
<div class="mb-3">
<label class="form-label"><strong>Appointment Date:</strong></label>
<input type="date" name="appointment_date" class="form-control" required>
</div>
<div class="mb-3">
<label class="form-label"><strong>Appointment Time:</strong></label>
<input type="time" name="appointment_time" class="form-control" required>
</div>
<div class="d-grid gap-2 d-md-flex">
<button type="submit" name="add_appointment" class="btn btn-success">
<i class="bi bi-calendar-plus"></i> Schedule Appointment
</button>
<a href="view_appointments.php" class="btn btn-secondary"><i class="bi bi-arrow-left"></i> Cancel</a>
</div>
</form>
</div>
</div>
</div>
<?php $conn->close(); ?>
edit_appointmet.php
<?php
include '../../db/db.php';
// Check if the appointment ID is provided
if (isset($_GET['id'])) {
$appointment_id = intval($_GET['id']);
// Fetch appointment details
$sql = "SELECT * FROM appointments WHERE id = $appointment_id";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$appointment = $result->fetch_assoc();
} else {
echo "<script>alert('Appointment not found!'); window.location='view_appointments.php';</script>";
exit;
}
} else {
echo "<script>alert('Invalid request!'); window.location='view_appointments.php';</script>";
exit;
}
// Update appointment when form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['update_appointment'])) {
$status = $conn->real_escape_string($_POST['status']);
// Update the status in the database
$update_sql = "UPDATE appointments SET status = '$status' WHERE id = $appointment_id";
if ($conn->query($update_sql) === TRUE) {
echo "<script>alert('Appointment status updated successfully!'); window.location='view_appointments.php';</script>";
exit;
} else {
$error = "Error updating appointment: " . $conn->error;
}
}
?>
<div class="content">
<div class="container mt-4">
<h2 class="text-center text-primary">✏️ Edit Appointment</h2>
<?php if (isset($error)): ?>
<div class="alert alert-danger"><?= $error; ?></div>
<?php endif; ?>
<div class="card p-4">
<form action="" method="post">
<div class="mb-3">
<label class="form-label">Patient Name:</label>
<input type="text" name="patient_name" class="form-control" value="<?= htmlspecialchars($appointment['patient_name']); ?>" readonly>
</div>
<div class="mb-3">
<label class="form-label">Appointment Date:</label>
<input type="text" name="appointment_date" class="form-control" value="<?= htmlspecialchars($appointment['appointment_date']); ?>" readonly>
</div>
<div class="mb-3">
<label class="form-label">Appointment Time:</label>
<input type="text" name="appointment_time" class="form-control" value="<?= htmlspecialchars($appointment['appointment_time']); ?>" readonly>
</div>
<div class="mb-3">
<label class="form-label">Status:</label>
<select name="status" class="form-select">
<option value="Scheduled" <?= $appointment['status'] == "Scheduled" ? 'selected' : '' ?>>Scheduled</option>
<option value="Confirmed" <?= $appointment['status'] == "Confirmed" ? 'selected' : '' ?>>Confirmed</option>
<option value="Completed" <?= $appointment['status'] == "Completed" ? 'selected' : '' ?>>Completed</option>
<option value="Cancelled" <?= $appointment['status'] == "Cancelled" ? 'selected' : '' ?>>Cancelled</option>
</select>
</div>
<button type="submit" name="update_appointment" class="btn btn-primary">Update Appointment</button>
<a href="view_appointments.php" class="btn btn-secondary">Cancel</a>
</form>
</div>
</div>
</div>
<?php $conn->close(); ?>
delete_patient.php
<?php
include '../../db/db.php'; // Database connection
// Check if an appointment ID is provided
if (!isset($_GET['id']) || empty($_GET['id'])) {
echo "<script>alert('Invalid request!'); window.location='view_appointments.php';</script>";
exit;
}
$appointment_id = intval($_GET['id']);
// Delete the appointment from the database
$delete_sql = "DELETE FROM appointments WHERE id = $appointment_id";
if ($conn->query($delete_sql) === TRUE) {
echo "<script>alert('Appointment deleted successfully!'); window.location='view_appointments.php';</script>";
} else {
echo "<script>alert('Error deleting appointment: " . $conn->error . "'); window.location='view_appointments.php';</script>";
}
$conn->close();
?>
view_appintment.php
<?php
include '../../db/db.php';
// Handle search and filter inputs
$search = isset($_GET['search']) ? $conn->real_escape_string($_GET['search']) : "";
$doctor_filter = isset($_GET['doctor_id']) ? intval($_GET['doctor_id']) : "";
$status_filter = isset($_GET['status']) ? $conn->real_escape_string($_GET['status']) : "";
// SQL Query to fetch appointments with filters
$sql = "SELECT appointments.*, doctors.name AS doctor_name
FROM appointments
JOIN doctors ON appointments.doctor_id = doctors.id
WHERE (patient_name LIKE '%$search%' OR '$search' = '')
AND (appointments.doctor_id = '$doctor_filter' OR '$doctor_filter' = '')
AND (appointments.status = '$status_filter' OR '$status_filter' = '')
ORDER BY appointment_date DESC";
$result = $conn->query($sql);
// Fetch doctors for the filter dropdown
$doctors = $conn->query("SELECT id, name FROM doctors ORDER BY name ASC");
?>
<div class="content">
<div class="container mt-4">
<h2 class="text-center text-primary">📅 Appointments List</h2>
<!-- Search & Filter Form -->
<form class="row g-3 mb-3" method="GET">
<div class="col-md-4">
<input type="text" name="search" class="form-control" placeholder="Search by Patient Name" value="<?= htmlspecialchars($search) ?>">
</div>
<div class="col-md-3">
<select name="doctor_id" class="form-select">
<option value="">Filter by Doctor</option>
<?php while ($doc = $doctors->fetch_assoc()): ?>
<option value="<?= $doc['id'] ?>" <?= $doctor_filter == $doc['id'] ? 'selected' : '' ?>><?= htmlspecialchars($doc['name']) ?></option>
<?php endwhile; ?>
</select>
</div>
<div class="col-md-3">
<select name="status" class="form-select">
<option value="">Filter by Status</option>
<option value="Pending" <?= $status_filter == "Pending" ? 'selected' : '' ?>>Pending</option>
<option value="Confirmed" <?= $status_filter == "Confirmed" ? 'selected' : '' ?>>Confirmed</option>
<option value="Cancelled" <?= $status_filter == "Cancelled" ? 'selected' : '' ?>>Cancelled</option>
</select>
</div>
<div class="col-md-2">
<button type="submit" class="btn btn-primary w-100">🔍 Search</button>
</div>
</form>
<!-- Appointments Table -->
<?php if ($result->num_rows > 0): ?>
<table class="table table-striped table-hover">
<thead class="table-dark">
<tr>
<th>ID</th>
<th>Patient</th>
<th>Doctor</th>
<th>Date</th>
<th>Time</th>
<th>Status</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php while ($row = $result->fetch_assoc()): ?>
<tr>
<td><?= $row['id'] ?></td>
<td><?= htmlspecialchars($row['patient_name']) ?></td>
<td><?= htmlspecialchars($row['doctor_name']) ?></td>
<td><?= date("d M Y", strtotime($row['appointment_date'])) ?></td>
<td><?= date("h:i A", strtotime($row['appointment_time'])) ?></td>
<td>
<span class="badge bg-<?= $row['status'] == 'Scheduled' ? 'info' : ($row['status'] == 'Completed' ? 'success' : 'danger') ?>">
<?= $row['status'] ?>
</span>
</td>
<td>
<a href="edit_appointment.php?id=<?= $row['id'] ?>" class="btn btn-primary btn-sm">✏️ Edit</a>
<a href="delete_appointment.php?id=<?= $row['id'] ?>" class="btn btn-danger btn-sm" onclick="return confirm('Are you sure you want to delete this appointment?');">🗑 Delete</a>
</td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
<?php else: ?>
<p class="alert alert-warning text-center">No appointments found.</p>
<?php endif; ?>
</div>
</div>
<?php $conn->close(); ?>
confirm_appointment.php
<?php
require_once('../../db/db.php'); // Database connection
// ✅ Fetch only confirmed appointments
$sql = "SELECT a.*, d.name AS doctor_name, p.name AS patient_name
FROM appointments a
JOIN doctors d ON a.doctor_id = d.id
JOIN patients p ON a.patient_id = p.id
WHERE a.status = 'Confirmed'"; // Only confirmed status
$result = $conn->query($sql);
// ✅ Check if query execution was successful
if (!$result) {
die("Query failed: " . $conn->error);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Confirmed Appointments</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
</head>
<body>
<div class="container mt-4">
<h2 class="text-center">📅 Confirmed Appointments</h2>
<!-- Print Button -->
<button onclick="window.print();" class="btn btn-primary mb-3">🖨 Print</button>
<!-- Confirmed Appointments Table -->
<table class="table table-bordered">
<thead class="table-dark">
<tr>
<th>ID</th>
<th>Patient</th>
<th>Doctor</th>
<th>Date</th>
<th>Time</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php if ($result->num_rows > 0): ?>
<?php while ($row = $result->fetch_assoc()): ?>
<tr>
<td><?= $row['id']; ?></td>
<td><?= htmlspecialchars($row['patient_name']); ?></td>
<td><?= htmlspecialchars($row['doctor_name']); ?></td>
<td><?= $row['appointment_date']; ?></td>
<td><?= $row['appointment_time']; ?></td>
<td><a href="../billing/billing.php?id=<?= $row['id']; ?>" class="badge bg-success text-decoration-none text-light">
Confirmed</a></td></tr>
<?php endwhile; ?>
<?php else: ?>
<tr><td colspan="6" class="text-center">No confirmed appointments found.</td></tr>
<?php endif; ?>
</tbody>
</table>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
<?php $conn->close(); ?>