Creating a Library Management System involves several components, including a MySQL database schema, file structure, layout design using Bootstrap 5, and complete PHP scripts for user authentication and management. Below is a comprehensive guide to help you set up your system.

Project Introduction

The Library Management System is designed to facilitate the efficient management of library resources, including books, users, and borrowing records. This system provides a comprehensive platform for librarians and members to manage book inventories, track borrowing activities, and handle notifications and payments. By utilizing a MySQL database, the system ensures that all data related to users, books, and transactions is stored securely and can be accessed easily, making it an essential tool for modern libraries aiming to enhance their operational efficiency and user experience.

Project Objectives

  • To create a user-friendly interface for library members to search for and borrow books.
  • To enable librarians to manage book inventories, including adding, updating, and removing books.
  • To track borrowing records, including due dates and return dates, to ensure timely returns.
  • To implement a notification system to inform users about their borrowing status and fines.
  • To facilitate payment processing for any fines incurred by users.
  • To provide reporting features for librarians to analyze borrowing trends and library usage.

Project Modules

  1. User Management: Handles user registration, login, and role-based access control for admins, librarians, and members.
  2. Book Management: Allows librarians to add, update, and delete book records, as well as manage book categories.
  3. Borrowing Management: Manages the borrowing process, including tracking borrow dates, due dates, and return dates.
  4. Notification System: Sends notifications to users regarding their borrowing status, due dates, and fines.
  5. Payment Processing: Manages payments for fines and other fees incurred by users.
  6. Reporting Module: Provides reports on library usage, borrowing trends, and user activity for administrative purposes.

1. MySQL Database Schema


CREATE DATABASE library_management;
USE library_management;
-- Table for users
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL,
email VARCHAR(100) NOT NULL UNIQUE,
role ENUM('admin', 'librarian', 'member') DEFAULT 'member',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Table for books
CREATE TABLE books (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
author VARCHAR(255) NOT NULL,
isbn VARCHAR(20) NOT NULL UNIQUE,
category VARCHAR(100),
status ENUM('available', 'borrowed', 'reserved') DEFAULT 'available',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Table for borrowing records
CREATE TABLE borrowings (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
book_id INT NOT NULL,
borrow_date DATE NOT NULL,
due_date DATE NOT NULL,
return_date DATE,
fine DECIMAL(10, 2) DEFAULT 0.00,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
FOREIGN KEY (book_id) REFERENCES books(id) ON DELETE CASCADE
);
-- Table for notifications
CREATE TABLE notifications (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
message TEXT NOT NULL,
is_read BOOLEAN DEFAULT FALSE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
);
-- Table for payments
CREATE TABLE payments (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
amount DECIMAL(10, 2) NOT NULL,
payment_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
);

2. File and Folder Structure


library_management_system/

├── config/
│ └── db.php

├── public/
│ ├── index.php
│ ├── login.php
│ ├── register.php
│ ├── dashboard.php
│ ├── books.php
│ ├── borrow.php
│ ├── return.php
│ ├── notifications.php
│ ├── payments.php
│ ├── reports.php
│ └── search.php

├── includes/
│ ├── header.php
│ ├── footer.php
│ └── functions.php

├── assets/
│ ├── css/
│ │ └── styles.css
│ ├── js/
│ │ └── scripts.js
│ └── images/

└── vendor/
└── (Bootstrap and other libraries)

3. Layout Based Design with Bootstrap 5

Header (includes/header.php)


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="assets/css/styles.css">
<title>Library Management System</title>
</head>
<body>
<div class="container">
<header class="my-4">
<h1>Library Management System</h1>
</header>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container-fluid">
<a class="navbar-brand" href="dashboard.php">Dashboard</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="books.php">Books</a>
</li>
<li class="nav-item">
<a class="nav-link" href="borrow.php">Borrow</a>
</li>
<li class="nav-item">
<a class="nav-link" href="return.php">Return</a>
</li>
<li class="nav-item">
<a class="nav-link" href="notifications.php">Notifications</a>
</li>
<li class="nav-item">
<a class="nav-link" href="payments.php">Payments</a>
</li>
<li class="nav-item">
<a class="nav-link" href="reports.php">Reports</a>
</li>
<li class="nav-item">
<a class="nav-link" href="search.php">Search</a>
</li>
<li class="nav-item">
<a class="nav-link" href="logout.php">Logout</a>
</li>
</ul>
</div>
</div>
</nav>
<main class="my-4">

Footer (includes/footer.php)


</main>
<footer class="text-center my-4">
<p>© 2023 Library Management System. All rights reserved.</p>
</footer>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>

4. PHP Code for User Authentication

Registration (public/register.php)


<?php
require '../config/db.php';
require '../includes/header.php';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$username = $_POST['username'];
$email = $_POST['email'];
$password = password_hash($_POST['password'], PASSWORD_DEFAULT);
$role = $_POST['role'];
$stmt = $conn->prepare("INSERT INTO users (username, email, password, role) VALUES (?, ?, ?, ?)");
$stmt->bind_param("ssss", $username, $email, $password, $role);
$stmt->execute();
$stmt->close();
header("Location: login.php");
}
?>
<form method="POST" action="">
<div class="mb-3">
<label for="username" class="form-label">Username</label>
<input type="text" class="form-control" id="username" name="username" required>
</div>
<div class="mb-3">
<label for="email" class="form-label">Email</label>
<input type="email" class="form-control" id="email" name="email" required>
</div>
<div class="mb-3">
<label for="password" class="form-label">Password</label>
<input type="password" class="form-control" id="password" name="password" required>
</div>
<div class="mb-3">
<label for="role" class="form-label">Role</label>
<select class="form-select" id="role" name="role">
<option value="member">Member</option>
<option value="librarian">Librarian</option>
<option value="admin">Admin</option>
</select>
</div>
<button type="submit" class="btn btn-primary">Register</button>
</form>
<?php require '../includes/footer.php'; ?>

Login (public/login.php)


<?php
session_start();
require '../config/db.php';
require '../includes/header.php';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$username = $_POST['username'];
$password = $_POST['password'];
$stmt = $conn->prepare("SELECT * FROM users WHERE username = ?");
$stmt->bind_param("s", $username);
$stmt->execute();
$result = $stmt->get_result();
$user = $result->fetch_assoc();
if ($user && password_verify($password, $user['password'])) {
$_SESSION['user_id'] = $user['id'];
$_SESSION['role'] = $user['role'];
header("Location: dashboard.php");
} else {
echo "Invalid credentials.";
}
}
?>
<form method="POST" action="">
<div class="mb-3">
<label for="username" class="form-label">Username</label>
<input type="text" class="form-control" id="username" name="username" required>
</div>
<div class="mb-3">
<label for="password" class="form-label">Password</label>
<input type="password" class="form-control" id="password" name="password" required>
</div>
<button type="submit" class="btn btn-primary">Login</button>
</form>
<?php require '../includes/footer.php'; ?>

5. Additional PHP Scripts

Dashboard (public/dashboard.php)


<?php
session_start();
require '../config/db.php';
require '../includes/header.php';
if (!isset($_SESSION['user_id'])) {
header("Location: login.php");
exit();
}
$user_id = $_SESSION['user_id'];
$stmt = $conn->prepare("SELECT * FROM users WHERE id = ?");
$stmt->bind_param("i", $user_id);
$stmt->execute();
$result = $stmt->get_result();
$user = $result->fetch_assoc();
?>
<h2>Welcome, <?php echo $user['username']; ?></h2>
<p>Your role: <?php echo ucfirst($user['role']); ?></p>
<?php require '../includes/footer.php'; ?>

Books Management (public/books.php)


<?php
require '../config/db.php';
require '../includes/header.php';
$stmt = $conn->prepare("SELECT * FROM books");
$stmt->execute();
$result = $stmt->get_result();
$books = $result->fetch_all(MYSQLI_ASSOC);
?>
<h3>Library Books</h3>
<a href="add_book.php" class="btn btn-primary">Add New Book</a>
<table class="table">
<thead>
<tr>
<th>Title</th>
<th>Author</th>
<th>ISBN</th>
<th>Category</th>
<th>Status</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($books as $book): ?>
<tr>
<td><?php echo $book['title']; ?></td>
<td><?php echo $book['author']; ?></td>
<td><?php echo $book['isbn']; ?></td>
<td><?php echo $book['category']; ?></td>
<td><?php echo ucfirst($book['status']); ?></td>
<td>
<a href="edit_book.php?id=<?php echo $book['id']; ?>" class="btn btn-warning">Edit</a>
<a href="delete_book.php?id=<?php echo $book['id']; ?>" class="btn btn-danger">Delete</a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php require '../includes/footer.php'; ?>

Add Book (public/add_book.php)


<?php
require '../config/db.php';
require '../includes/header.php';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$title = $_POST['title'];
$author = $_POST['author'];
$isbn = $_POST['isbn'];
$category = $_POST['category'];
$stmt = $conn->prepare("INSERT INTO books (title, author, isbn, category) VALUES (?, ?, ?, ?)");
$stmt->bind_param("ssss", $title, $author, $isbn, $category);
$stmt->execute();
$stmt->close();
header("Location: books.php");
}
?>
<form method="POST" action="">
<div class="mb-3">
<label for="title" class="form-label">Title</label>
<input type="text" class="form-control" id="title" name="title" required>
</div>
<div class="mb-3">
<label for="author" class="form-label">Author</label>
<input type="text" class="form-control" id="author" name="author" required>
</div>
<div class="mb-3">
<label for="isbn" class="form-label">ISBN</label>
<input type="text" class="form-control" id="isbn" name="isbn" required>
</div>
<div class="mb-3">
<label for="category" class="form-label">Category</label>
<input type="text" class="form-control" id="category" name="category" required>
</div>
<button type="submit" class="btn btn-primary">Add Book</button>
</form>
<?php require '../includes/footer.php'; ?>

Edit Book (public/edit_book.php)


<?php
require '../config/db.php';
require '../includes/header.php';
if (isset($_GET['id'])) {
$book_id = $_GET['id'];
$stmt = $conn->prepare("SELECT * FROM books WHERE id = ?");
$stmt->bind_param("i", $book_id);
$stmt->execute();
$result = $stmt->get_result();
$book = $result->fetch_assoc();
}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$title = $_POST['title'];
$author = $_POST['author'];
$isbn = $_POST['isbn'];
$category = $_POST['category'];
$stmt = $conn->prepare("UPDATE books SET title = ?, author = ?, isbn = ?, category = ? WHERE id = ?");
$stmt->bind_param("ssssi", $title, $author, $isbn, $category, $book_id);
$stmt->execute();
$stmt->close();
header("Location: books.php");
}
?>
<form method="POST" action="">
<div class="mb-3">
<label for="title" class="form-label">Title</label>
<input type="text" class="form-control" id="title" name="title" value="<?php echo $book['title']; ?>" required>
</div>
<div class="mb-3">
<label for="author" class="form-label">Author</label>
<input type="text" class="form-control" id="author" name="author" value="<?php echo $book['author']; ?>" required>
</div>
<div class="mb-3">
<label for="isbn" class="form-label">ISBN</label>
<input type="text" class="form-control" id="isbn" name="isbn" value="<?php echo $book['isbn']; ?>" required>
</div>
<div class="mb-3">
<label for="category" class="form-label">Category</label>
<input type="text" class="form-control" id="category" name="category" value="<?php echo $book['category']; ?>" required>
</div>
<button type="submit" class="btn btn-primary">Update Book</button>
</form>
<?php require '../includes/footer.php'; ?>

Delete Book (public/delete_book.php)


<?php
require '../config/db.php';
if (isset($_GET['id'])) {
$book_id = $_GET['id'];
$stmt = $conn->prepare("DELETE FROM books WHERE id = ?");
$stmt->bind_param("i", $book_id);
$stmt->execute();
$stmt->close();
header("Location: books.php");
}
?>

Borrow Book (public/borrow.php)


<?php
require '../config/db.php';
require '../includes/header.php';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$user_id = $_SESSION['user_id'];
$book_id = $_POST['book_id'];
$borrow_date = date('Y-m-d');
$due_date = date('Y-m-d', strtotime('+14 days'));
$stmt = $conn->prepare("INSERT INTO borrowings (user_id, book_id, borrow_date, due_date) VALUES (?, ?, ?, ?)");
$stmt->bind_param("iiss", $user_id, $book_id, $borrow_date, $due_date);
$stmt->execute();
$stmt->close();
header("Location: dashboard.php");
}
?>
<form method="POST" action="">
<div class="mb-3">
<label for="book_id" class="form-label">Book ID</label>
<input type="number" class="form-control" id="book_id" name="book_id" required>
</div>
<button type="submit" class="btn btn-primary">Borrow Book</button>
</form>
<?php require '../includes/footer.php'; ?>

Return Book (public/return.php)


<?php
require '../config/db.php';
require '../includes/header.php';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$borrowing_id = $_POST['borrowing_id'];
$return_date = date('Y-m-d');
$stmt = $conn->prepare("UPDATE borrowings SET return_date = ? WHERE id = ?");
$stmt->bind_param("si", $return_date, $borrowing_id);
$stmt->execute();
$stmt->close();
header("Location: dashboard.php");
}
?>
<form method="POST" action="">
<div class="mb-3">
<label for="borrowing_id" class="form-label">Borrowing ID</label>
<input type="number" class="form-control" id="borrowing_id" name="borrowing_id" required>
</div>
<button type="submit" class="btn btn-primary">Return Book</button>
</form>
<?php require '../includes/footer.php'; ?>

Notifications (public/notifications.php)


<?php
require '../config/db.php';
require '../includes/header.php';
$user_id = $_SESSION['user_id'];
$stmt = $conn->prepare("SELECT * FROM notifications WHERE user_id = ?");
$stmt->bind_param("i", $user_id);
$stmt->execute();
$result = $ $stmt->get_result();
$notifications = $result->fetch_all(MYSQLI_ASSOC);
?>
<h3>Your Notifications</h3>
<table class="table">
<thead>
<tr>
<th>Message</th>
<th>Status</th>
<th>Date</th>
</tr>
</thead>
<tbody>
<?php foreach ($notifications as $notification): ?>
<tr>
<td><?php echo $notification['message']; ?></td>
<td><?php echo $notification['is_read'] ? 'Read' : 'Unread'; ?></td>
<td><?php echo $notification['created_at']; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php require '../includes/footer.php'; ?>

Payments (public/payments.php)


<?php
require '../config/db.php';
require '../includes/header.php';
$user_id = $_SESSION['user_id'];
$stmt = $conn->prepare("SELECT * FROM payments WHERE user_id = ?");
$stmt->bind_param("i", $user_id);
$stmt->execute();
$result = $stmt->get_result();
$payments = $result->fetch_all(MYSQLI_ASSOC);
?>
<h3>Your Payment History</h3>
<table class="table">
<thead>
<tr>
<th>Amount</th>
<th>Date</th>
</tr>
</thead>
<tbody>
<?php foreach ($payments as $payment): ?>
<tr>
<td><?php echo $payment['amount']; ?></td>
<td><?php echo $payment['payment_date']; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php require '../includes/footer.php'; ?>

Reports (public/reports.php)


<?php
require '../config/db.php';
require '../includes/header.php';
$stmt = $conn->prepare("SELECT b.title, COUNT(br.id) AS borrow_count FROM books b LEFT JOIN borrowings br ON b.id = br.book_id GROUP BY b.id");
$stmt->execute();
$result = $stmt->get_result();
$reports = $result->fetch_all(MYSQLI_ASSOC);
?>
<h3>Library Reports</h3>
<table class="table">
<thead>
<tr>
<th>Book Title</th>
<th>Borrow Count</th>
</tr>
</thead>
<tbody>
<?php foreach ($reports as $report): ?>
<tr>
<td><?php echo $report['title']; ?></td>
<td><?php echo $report['borrow_count']; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php require '../includes/footer.php'; ?>

Search (public/search.php)


<?php
require '../config/db.php';
require '../includes/header.php';
if (isset($_GET['query'])) {
$query = $_GET['query'];
$stmt = $conn->prepare("SELECT * FROM books WHERE title LIKE ? OR author LIKE ?");
$searchTerm = "%$query%";
$stmt->bind_param("ss", $searchTerm, $searchTerm);
$stmt->execute();
$result = $stmt->get_result();
$books = $result->fetch_all(MYSQLI_ASSOC);
}
?>
<form method="GET" action="">
<div class="mb-3">
<label for="query" class="form-label">Search</label>
<input type="text" class="form-control" id="query" name="query" required>
</div>
<button type="submit" class="btn btn-primary">Search</button>
</form>
<?php if (isset($books)): ?>
<h3>Search Results</h3>
<table class="table">
<thead>
<tr>
<th>Title</th>
<th>Author</th>
<th>ISBN</th>
<th>Category</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php foreach ($books as $book): ?>
<tr>
<td><?php echo $book['title']; ?></td>
<td><?php echo $book['author']; ?></td>
<td><?php echo $book['isbn']; ?></td>
<td><?php echo $book['category']; ?></td>
<td><?php echo ucfirst($book['status']); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
<?php require '../includes/footer.php'; ?>

Logout (public/logout.php)


<?php
session_start();
session _destroy();
header("Location: login.php");
?>

6. Additional Features to Consider

User Activity Tracking: Implement logging of user activities for auditing purposes.

Advanced Search Options: Enhance the search functionality with filters for categories, publication dates, etc.

Reservation System: Allow users to reserve books that are currently checked out.

User Feedback System: Implement a system for users to provide feedback on books and library services.

Mobile App Integration: Consider developing a mobile app for easier access to library services.

7. Security Measures

Data Encryption: Use encryption for sensitive data, especially passwords and payment information.

Session Security: Implement measures to prevent session hijacking and fixation.

Regular Backups: Schedule regular backups of the database to prevent data loss.

8. Testing and Deployment

Functional Testing: Ensure all features work as intended through rigorous testing.

User Acceptance Testing: Gather feedback from actual users to refine the system.

Deployment: Choose a reliable hosting service and deploy the application, ensuring all configurations are optimized for performance.

9. Documentation

User Guide: Create a comprehensive user guide to assist users in navigating the system.

Developer Documentation: Document the codebase and architecture for future reference and maintenance.

10. Future Enhancements

Integration with E-Books: Consider adding support for e-books and digital resources.

Social Media Integration: Allow users to share their reading lists or reviews on social media platforms.

Gamification: Introduce gamification elements to encourage user engagement, such as rewards for frequent borrowing.

This structured approach will help you build a robust Library Management System that meets the needs of users and adapts to future requirements.