How to develop Projects(HMS) Using PHP and Mysql
Part 11
Module 3: Add Patient Details
✅ Purpose:
Allow hospital staff (like a receptionist or admin) to add new patient records
into the database for future treatment, billing, and reference.
🗄️ Database Table Suggestion:
A simple MySQL table might look like:
CREATE TABLE patients (
id INT AUTO_INCREMENT PRIMARY KEY,
full_name VARCHAR(100),
age INT,
dob DATE,
gender ENUM('Male', 'Female', 'Other'),
address TEXT,
contact_number VARCHAR(15),
email VARCHAR(100),
blood_group VARCHAR(5),
medical_conditions TEXT,
allergies TEXT,
medications TEXT,
emergency_contact_name VARCHAR(100),
admission_date DATE,
reason_for_admission TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
/hospital_management_system/
├── /patients/
│ ├── add_patient.php # Form to add a new patient
│ ├── insert_patient.php # PHP logic to save patient data to DB
│ ├── list_patients.php # Display all patient records
│ ├── edit_patient.php # Edit existing patient data
│ └── delete_patient.php # Delete a patient record
│
├── /assets/
│ ├── /css/
│ │
└── style.css # Styling
for the module
│ └── /images/
│ └── patient_photos/ # Folder for uploaded patient
documents/photos
🏥 Patient Module —
Features
✅ 1. Add New Patient
- Allows
hospital staff to register new patients.
- Capture
personal, contact, and medical history.
- Auto-generate
Patient ID.
- Upload
supporting documents (ID proof, previous prescriptions, medical records).
✅ 2. Edit Patient Details
- Update
patient records (name, contact, address, medical details).
- Assign
or change doctor.
- Update
admission or discharge status.
- Update
room/ward info for admitted patients.
✅ 3. Delete Patient Record
- Remove
incorrect or duplicate patient entries.
- Soft
delete or permanent delete option for data safety.
✅ 4. View Patient List
- Display
all registered patients in a table.
- Search
filter: by name, ID, date, status.
- Pagination
support for large databases.
✅ 5. Patient Profile View
- Detailed
view of individual patient info.
- Medical
history, visit history, prescriptions, allergies.
- Billing
& payment status (optional).
📂 /patients/add_patient.php
<?php include('../config/db_connect.php');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Add New Patient</title>
</head>
<body>
<h2>Add New
Patient</h2>
<form action="insert_patient.php"
method="POST">
<label>Full
Name:</label><input type="text" name="full_name"
required><br>
<label>Age:</label><input
type="number" name="age" required><br>
<label>Gender:</label>
<select name="gender"
required>
<option value="Male">Male</option>
<option value="Female">Female</option>
</select><br>
<label>Address:</label><textarea name="address"
required></textarea><br>
<label>Contact
Number:</label><input type="text" name="contact_number"
required><br>
<label>Status:</label>
<select name="status"
required>
<option value="Inpatient">Inpatient</option>
<option value="Outpatient">Outpatient</option>
</select><br>
<button type="submit">Add
Patient</button>
</form>
</body>
</html>
📂 /patients/insert_patient.php
<?php
include('../config/db_connect.php');
if ($_SERVER['REQUEST_METHOD']
== 'POST') {
$full_name = $_POST['full_name'];
$age = $_POST['age'];
$gender = $_POST['gender'];
$address = $_POST['address'];
$contact_number = $_POST['contact_number'];
$status = $_POST['status'];
$sql = "INSERT INTO patients
(full_name, age, gender, address, contact_number, status)
VALUES ('$full_name', '$age', '$gender',
'$address', '$contact_number', '$status')";
if ($conn->query($sql) === TRUE) {
echo "Patient added successfully!
<a href='list_patients.php'>View Patients</a>";
} else {
echo "Error: " . $conn->error;
}
}
?>
📂 /patients/list_patients.php
<?php
include('../config/db_connect.php');
$result = $conn->query("SELECT
* FROM patients");
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Patient List</title>
</head>
<body>
<h2>Patient
List</h2>
<a href="add_patient.php">Add
New Patient</a><br><br>
<table border="1">
<tr>
<th>ID</th><th>Name</th><th>Age</th><th>Gender</th><th>Contact</th><th>Status</th><th>Actions</th>
</tr>
<?php while($row = $result->fetch_assoc()):
?>
<tr>
<td><?= $row['id']; ?></td>
<td><?= $row['full_name']; ?></td>
<td><?= $row['age']; ?></td>
<td><?= $row['gender']; ?></td>
<td><?= $row['contact_number'];
?></td>
<td><?= $row['status']; ?></td>
<td>
<a href="edit_patient.php?id=<?=
$row['id']; ?>">Edit</a> |
<a href="delete_patient.php?id=<?=
$row['id']; ?>" onclick="return confirm('Are you sure?')">Delete</a>
</td>
</tr>
<?php endwhile; ?>
</table>
</body>
</html>
📂 /patients/edit_patient.php
<?php
include('../config/db_connect.php');
$id = $_GET['id'];
$result = $conn->query("SELECT
* FROM patients WHERE id = $id");
$row = $result->fetch_assoc();
if ($_SERVER['REQUEST_METHOD']
== 'POST') {
$full_name = $_POST['full_name'];
$age = $_POST['age'];
$gender = $_POST['gender'];
$address = $_POST['address'];
$contact_number = $_POST['contact_number'];
$status = $_POST['status'];
$sql = "UPDATE patients SET
full_name='$full_name', age='$age', gender='$gender', address='$address',
contact_number='$contact_number',
status='$status' WHERE id=$id";
if ($conn->query($sql) === TRUE) {
header('Location: list_patients.php');
} else {
echo "Error updating record:
" . $conn->error;
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Edit Patient</title>
</head>
<body>
<h2>Edit
Patient</h2>
<form method="POST">
<label>Full
Name:</label><input type="text" name="full_name"
value="<?= $row['full_name']; ?>" required><br>
<label>Age:</label><input
type="number" name="age" value="<?= $row['age'];
?>" required><br>
<label>Gender:</label>
<select name="gender"
required>
<option value="Male" <?=
$row['gender'] == 'Male' ? 'selected' : '' ?>>Male</option>
<option value="Female" <?=
$row['gender'] == 'Female' ? 'selected' : '' ?>>Female</option>
</select><br>
<label>Address:</label><textarea
name="address" required><?= $row['address']; ?></textarea><br>
<label>Contact
Number:</label><input type="text" name="contact_number"
value="<?= $row['contact_number']; ?>" required><br>
<label>Status:</label>
<select name="status"
required>
<option value="Inpatient" <?=
$row['status'] == 'Inpatient' ? 'selected' : '' ?>>Inpatient</option>
<option value="Outpatient"
<?= $row['status'] == 'Outpatient' ? 'selected' : '' ?>>Outpatient</option>
</select><br>
<button type="submit">Update
Patient</button>
</form>
</body>
</html>
📂 /patients/delete_patient.php
<?php
include('../config/db_connect.php');
$id = $_GET['id'];
$sql = "DELETE FROM
patients WHERE id=$id";
if ($conn->query($sql)
=== TRUE) {
header('Location: list_patients.php');
} else {
echo "Error deleting record: " . $conn->error;
}
?>
✅ Summary:
- add_patient.php
— shows a form.
- insert_patient.php
— saves data to DB.
- list_patients.php
— shows all records.
- edit_patient.php
— allows updating a record.
- delete_patient.php
— deletes a record.