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
Department Store Management System(DSMS) using C++
A Department Store Management System (DSMS) in C++ is a software tool used to manage the inventory and sales of items in a department store. In this article, we will be discussing the implementation of a DSMS on a command-line user interface using C++. The system allows various features to admin suc
8 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
11 min read
How to Design ER Diagram for a Hospital Management System
Healthcare management is a crucial function that comes as the backbone of hospital management. An ER (Entity-Relationship) Diagram therefore functions as a foundation for the organization and visualization of the various entities, attributes, and relationships within a system. In this article, the s
7 min read
Employee Database Management System using HTML CSS and JavaScript
In this article, we will be building an employee database management system using JavaScript. Employee Database Management System is the collection of Employees' data like names, first and last, email, contact numbers, salary and date of birth, etc. It provides an easy way to access and manage the l
7 min read
How to make a Todo App using PHP & MySQL ?
To create a Todo App using PHP and MySQL, you'll first need to set up a MySQL database to store the tasks. Then, use PHP to create a web interface where users can add, edit, and delete tasks. PHP will handle the backend logic, such as connecting to the database and performing CRUD operations. Finall
4 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
13 min read
How to Insert JSON data into MySQL database using PHP?
To insert JSON data into MySQL database using PHP, use the json_decode function in PHP to convert JSON object into an array that can be inserted into the database. Here, we are going to see how to insert JSON data into MySQL database using PHP through the XAMPP server in a step-by-step way. JSON Str
3 min read
Building a REST API with PHP and MySQL
This brief tutorial is a step-by-step guide on how to develop a REST API using PHP and MySQL. REST API will implement HTTP commands (Get, Post, Put, DELETE) and response will be in form of JSON. For development setup, we will be using the XAMPP while for testing of the API, we will use the Postman a
4 min read
How to make a connection with MySQL server using PHP ?
MySQL is a widely used database management system that may be used to power a wide range of projects. One of its main selling features is its capacity to manage large amounts of data without breaking a sweat. There are two approaches that can be used to connect MySQL and PHP code, which are mentione
3 min read