Create a Hospital Management System using PHP and MySQL
Last Updated :
26 Jul, 2024
The Hospital Management System (HMS) is a robust and efficient solution designed to streamline the processes within a healthcare facility. This project is built using PHP and MySQL, offering a user-friendly interface for managing patient information, appointments, and other essential aspects of hospital administration.
Preview
Approach
The approach involves creating a web-based system that utilizes PHP for server-side scripting and MySQL for the database. PHP handles the backend logic, while MySQL stores and retrieves data efficiently.
Steps to Create & Configure the Project
Step 1: Set Up a Local Development Environment.
- Install a web server (XAMPP, WampServer) to run PHP scripts.
- Set up a MySQL database for storing hospital data.
Step 2: Create Database and Tables
- Design the database schema to store patient information. In MySQL, create a database named hospital_management. Design tables, such as patients, to store relevant data like patient names, ages, and admission dates. Ensure to define appropriate data types, primary keys, and relationships.
CREATE DATABASE hospital_management;
USE hospital_management;
CREATE TABLE patients (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
age INT NOT NULL,
gender VARCHAR(10) NOT NULL,
address TEXT,
admission_date DATE NOT NULL
);
Step 3: Build PHP Scripts
- Develop PHP scripts to handle various functionalities. Create separate PHP files for different features, such as adding patients (add_patient.php) and viewing patient information (view_patients.php). Implement the logic for interacting with the database, processing form submissions, and rendering dynamic content.
PHP
// index.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content=
"width=device-width, initial-scale=1.0">
<title>Hospital Management System</title>
<style>
body {
background-color: #606060FF;
color: #D6ED17FF;
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
text-align: center;
}
h1 {
background-color: #D6ED17FF;
color: black !important;
padding: 20px;
margin-bottom: 0;
}
ul {
list-style-type: none;
padding: 0;
}
li {
display: inline-block;
margin: 10px;
}
a {
text-decoration: none;
color: #606060FF;
background-color: #D6ED17FF;
padding: 10px 20px;
border-radius: 5px;
}
a:hover {
background-color: #606060FF;
color: #D6ED17FF;
}
</style>
</head>
<body>
<h1>Hospital Management System</h1>
<ul>
<li>
<a href="add_patient.php">Add Patient</a>
</li>
<li>
<a href="view_patients.php">View Patients</a>
</li>
</ul>
</body>
</html>
- Defines the HTML structure for the home page. Provides links to "add_patient.php" and "view_patients.php." The embedded CSS styles enhance the appearance of the page.
PHP
// add_patient.php
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$name = $_POST['name'];
$age = $_POST['age'];
$gender = $_POST['gender'];
$address = $_POST['address'];
$admission_date = $_POST['admission_date'];
$conn = new mysqli('localhost', 'root', '', 'hospital_management');
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO patients (name, age, gender, address, admission_date)
VALUES ('$name', $age, '$gender', '$address', '$admission_date')";
if ($conn->query($sql) === TRUE) {
echo "Patient added successfully!";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content=
"width=device-width, initial-scale=1.0">
<title>Add Patient</title>
<style>
body {
background-color: #606060FF;
color: black;
font-weight: bolder;
font-family: Arial, sans-serif;
text-align: center;
margin: 0;
padding: 20px;
}
form {
max-width: 600px;
margin: 0 auto;
background-color: #D6ED17FF;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
h2{
background-color:#D6ED17FF;
padding: 1%;
border-radius: 35px;
}
label {
display: block;
margin: 10px 0;
text-align: left;
}
input[type="text"],
input[type="number"],
select,
textarea,
input[type="date"],
input[type="submit"] {
width: 100%;
margin: 5px 0;
padding: 10px;
border-radius: 5px;
border: 1px solid #606060FF;
box-sizing: border-box;
}
input[type="submit"] {
background-color: #606060FF;
color: #D6ED17FF;
cursor: pointer;
}
input[type="submit"]:hover {
background-color: #D6ED17FF;
color: #606060FF;
}
</style>
</head>
<body>
<h2>Add Patient</h2>
<form method="post" action="">
<label for="name">Name:</label>
<input type="text" name="name" required>
<label for="age">Age:</label>
<input type="number" name="age" required>
<label for="gender">Gender:</label>
<select name="gender">
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
<label for="address">Address:</label>
<textarea name="address"></textarea>
<label for="admission_date">Admission Date:</label>
<input type="date" name="admission_date" required>
<input type="submit" value="Add Patient">
</form>
</body>
</html>
- Contains PHP code to handle form submissions. Retrieves data from the submitted form (name, age, etc.) using the $_POST superglobal. Connects to the MySQL database, constructs an SQL query, and inserts the new patient record. The HTML part includes a form with input fields for patient details.
PHP
//view_patients.php
<?php
$conn = new mysqli('localhost', 'root', '', 'hospital_management');
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM patients";
$result = $conn->query($sql);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content=
"width=device-width, initial-scale=1.0">
<title>View Patients</title>
<style>
body {
background-color: #606060FF;
color: black;
font-family: Arial, sans-serif;
text-align: center;
margin: 0;
padding: 20px;
}
h2{
background-color:#D6ED17FF;
padding: 1%;
border-radius: 35px;
}
table {
background-color: #D6ED17FF;
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}
th, td {
border: 5px solid #606060FF;
padding: 10px;
color:black;
}
th {
background-color: #D6ED17FF;
}
</style>
</head>
<body>
<h2>View Patients</h2>
<table border="1">
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
<th>Gender</th>
<th>Address</th>
<th>Admission Date</th>
</tr>
<?php
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['address']}</td>
<td>{$row['admission_date']}</td>
</tr>";
}
?>
</table>
</body>
</html>
- Connects to the MySQL database and executes an SQL query to fetch all patient records. Utilizes a while loop to iterate through the result set and dynamically generates an HTML table displaying patient information.
Step 4: Adding Data to the Table
If you want to insert dummy data through mysql, You can us ethe following code:
INSERT INTO patients (name, age, gender, address, admission_date) VALUES
('John Doe', 35, 'Male', '123 Main St, Cityville', '2022-01-15'),
('Jane Smith', 28, 'Female', '456 Oak St, Townsville', '2022-01-16'),
('Bob Johnson', 45, 'Male', '789 Pine St, Villagetown', '2022-01-17'),
('Alice Brown', 32, 'Female', '987 Cedar St, Hamletville', '2022-01-18'),
('Charlie Wilson', 50, 'Male', '654 Birch St, Countryside', '2022-01-19');
Project Structure

Steps 5: Run the Application
- Start your local web server and MySQL database.
- Place the project files in the root directory of your web server.
- Access the application through the web browser (e.g., http://localhost/Hospital management/index.php).
Output:
Similar Reads
Create a Small CRM using PHP and MySQL CRM stands for Customer Relationship Management, which is a strategy for a set of practices, and a technology designed to manage and analyze customer interactions and data throughout the customer lifecycle. Manage contacts by adding, updating, and deleting information such as name, email, phone, and
6 min read
Hospital Management System using MEAN Stack The Hospital Management App is an important application that ensures seamless coordination to revolutionize healthcare administration. In this article, we will be creating a Hospital Management Website using the MEAN stack â i.e. MongoDB, Express, Angular, and Node.js, with step by step process for
15+ min read
Hospital Management Application using MERN Stack In the fast-paced world of healthcare, it's vital to manage hospital tasks effectively to ensure top-notch patient care. This article explores creating a strong Hospital Management App using the MERN stack â that's MongoDB, Express, React, and Node.js, breaking down the process for easier understand
15+ min read
Hospital Management System in C A Hospital Management System is software that facilitates its users some basic operations that take place in a typical hospital. This Hospital Management System in C language is a project for Hospitals having the following functionalities: Printing Hospital DataPrint Patients DataSort by Bed PriceSo
10 min read
Creating a Registration and Login System with PHP and MySQL A registration and login system is a fundamental component of many web applications and provides user authentication and security. This allows users to create an account log in with their login credentials and manage their session securely. By using PHP for server-side scripting and MYSQL for databa
12 min read