How to develop Projects(HMS) Using PHP and Mysql
Part 9
4️⃣ Hospital Management System
📌 Folder Structure:
│── /patients/
│ ├── add_patient.php # Add patient
│ ├── edit_patient.php # Edit patient details
│ ├── delete_patient.php # Delete patient
│ ├── patient_list.php # List all patients
Database Table:
CREATE TABLE patients (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
age INT NOT NULL,
gender ENUM('Male', 'Female', 'Other') NOT NULL,
contact VARCHAR(15),
height int(11),
weight int(11),
medical_history TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
📌 File: patients/add_patient.php
<?php
// Include database connection
include('../config/db.php');
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Get form data
$name = $_POST['name'];
$age = $_POST['age'];
$gender = $_POST['gender'];
$height = $_POST['height'];
$weight = $_POST['weight'];
$medical_history = $_POST['medical_history'];
// Prepare and execute the query
$sql = "INSERT INTO patients (name,
age, gender, height, weight, medical_history)
VALUES ('$name', '$age', '$gender',
'$height', '$weight', '$medical_history')";
if ($conn->query($sql) === TRUE) {
echo "New patient added
successfully.";
} else {
echo "Error: " . $sql . "<br>"
. $conn->error;
}
}
// Close connection
$conn->close();
?>
<!DOCTYPE html>
<html>
<head>
<title>Add Patient</title>
</head>
<body>
<h2>Add New Patient</h2>
<form method="post" action="">
<label>Name:</label>
<input type="text" name="name"
required><br><br>
<label>Age:</label>
<input type="number" name="age"
required><br><br>
<label>Gender:</label>
<select name="gender"
required>
<option value="Male">Male</option>
<option value="Female">Female</option>
<option value="Other">Other</option>
</select><br><br>
<label>Height (cm):</label>
<input type="number" name="height"><br><br>
<label>Weight (kg):</label>
<input type="number" name="weight"><br><br>
<label>Medical History:</label>
<textarea name="medical_history"></textarea><br><br>
<input type="submit" value="Add Patient">
</form>
</body>
</html>
Explanation:
1.
Database Connection:
Uses MySQLi to connect to hospital_management database.
2.
Form Handling:
When the form is submitted using POST, the patient details are retrieved and
inserted into the patients table.
3.
SQL Query Execution:
Uses INSERT INTO SQL query to add patient data.
4.
Basic HTML Form:
Allows users to input patient details.
📌 File: patients/edit_patient.php
// Include database connection
include('../config/db.php');
// Check if patient ID is set
if (!isset($_GET['id']))
{
die("Invalid request. No patient ID
provided.");
}
$patient_id = $_GET['id'];
// Fetch patient details
$sql = "SELECT *
FROM patients WHERE id = $patient_id";
$result = $conn->query($sql);
if ($result->num_rows
== 0) {
die("Patient not found.");
}
$patient = $result->fetch_assoc();
// Handle form submission
if ($_SERVER["REQUEST_METHOD"]
== "POST") {
// Get updated values
$name = $_POST['name'];
$age = $_POST['age'];
$gender = $_POST['gender'];
$height = $_POST['height'];
$weight = $_POST['weight'];
$medical_history = $_POST['medical_history'];
// Update query
$update_sql = "UPDATE patients SET
name = '$name',
age = '$age',
gender = '$gender',
height = '$height',
weight = '$weight',
medical_history = '$medical_history'
WHERE id = $patient_id";
if ($conn->query($update_sql) === TRUE)
{
echo "Patient details updated
successfully.";
// Redirect to another page if needed
} else {
echo "Error: " . $conn->error;
}
}
// Close connection
$conn->close();
?>
<!DOCTYPE html>
<html>
<head>
<title>Edit Patient</title>
</head>
<body>
<h2>Edit Patient Details</h2>
<form method="post" action="">
<label>Name:</label>
<input type="text" name="name"
value="<?php echo $patient['name']; ?>"
required><br><br>
<label>Age:</label>
<input type="number" name="age"
value="<?php echo $patient['age']; ?>"
required><br><br>
<label>Gender:</label>
<select name="gender"
required>
<option value="Male" <?php
if ($patient['gender'] == 'Male') echo 'selected'; ?>>Male</option>
<option value="Female"
<?php if ($patient['gender'] == 'Female') echo 'selected'; ?>>Female</option>
<option value="Other" <?php
if ($patient['gender'] == 'Other') echo 'selected'; ?>>Other</option>
</select><br><br>
<label>Height (cm):</label>
<input type="number" name="height"
value="<?php echo $patient['height'];
?>"><br><br>
<label>Weight (kg):</label>
<input type="number" name="weight"
value="<?php echo $patient['weight'];
?>"><br><br>
<label>Medical
History:</label>
<textarea name="medical_history"><?php
echo $patient['medical_history']; ?></textarea><br><br>
<input type="submit"
value="Update Patient">
</form>
</body>
</html>
Explanation:
1.
Database Connection:
Uses MySQLi to connect to hospital_management database.
2.
Fetching Patient Details:
Retrieves patient data from the database using SELECT.
3.
Updating Patient Details:
If the form is submitted, it updates the database using UPDATE.
4.
Basic HTML Form:
Pre-fills the form with existing patient data for editing.
📌 File: patients/delete_patient.php
<?php
// Include database connection
include('../config/db.php');
// Check if patient ID is set
if (!isset($_GET['id']))
{
die("Invalid request. No patient ID
provided.");
}
$patient_id = $_GET['id'];
// Delete query
$sql = "DELETE FROM
patients WHERE id = $patient_id";
if ($conn->query($sql)
=== TRUE) {
echo "Patient deleted
successfully.";
} else {
echo "Error: " . $conn->error;
}
// Close connection
$conn->close();
?>
Explanation:
1.
Database Connection:
Establishes a MySQLi connection to the hospital_management database.
2.
Checking Patient ID:
Ensures that a valid id is provided in the URL.
3.
Executing the DELETE Query:
Deletes the patient record from the patients table.
4.
Success/Error Message:
Displays a success or error message based on the query execution.
🔹 This script should be accessed via a
URL like:
delete_patient.php?id=1 (where 1 is the patient's ID to delete).
📌 File: patients/patient_list.php
<?php
// Include database connection
include('../config/db.php');
// Fetch all patients
$sql = "SELECT *
FROM patients ORDER BY id DESC";
$result = $conn->query($sql);
?>
<!DOCTYPE html>
<html>
<head>
<title>Patient List</title>
<style>
table {
width: 100%;
border-collapse: collapse;
}
table, th, td {
border: 1px solid black;
padding: 10px;
text-align: left;
}
th {
background-color: #f2f2f2;
}
.btn {
padding: 5px 10px;
text-decoration: none;
border: none;
cursor: pointer;
}
.edit {
background-color: #4CAF50;
color: white;
}
.delete {
background-color: #f44336;
color: white;
}
</style>
</head>
<body>
<h2>Patient List</h2>
<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
<th>Gender</th>
<th>Height</th>
<th>Weight</th>
<th>Medical
History</th>
<th>Actions</th>
</tr>
<?php
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc())
{
echo "<tr>
<td>" . $row['id']
. "</td>
<td>" . $row['name']
. "</td>
<td>" . $row['age']
. "</td>
<td>" . $row['gender']
. "</td>
<td>" . $row['height']
. "</td>
<td>" . $row['weight']
. "</td>
<td>" . $row['medical_history']
. "</td>
<td>
<a
href='edit_patient.php?id=" . $row['id'] . "' class='btn
edit'>Edit</a>
<a
href='delete_patient.php?id=" . $row['id'] . "' class='btn delete'
onclick='return confirm(\"Are you sure you want to delete this
patient?\")'>Delete</a>
</td>
</tr>";
}
} else {
echo "<tr><td
colspan='8'>No patients found.</td></tr>";
}
?>
</table>
</body>
</html>
<?php
// Close connection
$conn->close();
?>
Explanation:
1.
Database Connection:
Connects to the hospital_management database using MySQLi.
2.
Fetching Patients:
Retrieves all records from the patients table in descending order.
3.
Displaying Data:
Outputs patient records in an HTML table.
4.
Edit & Delete Options:
o Edit:
Links to edit_patient.php?id={patient_id} for updating patient details.
o Delete:
Links to delete_patient.php?id={patient_id} with a confirmation alert before
deletion.