Current Location: Home> Latest Articles> PHP Online Rental System Tutorial: Complete Guide from Database to Frontend

PHP Online Rental System Tutorial: Complete Guide from Database to Frontend

M66 2025-11-05

Overview of the PHP Online Rental System

With the rapid growth of the internet, many traditional rental businesses are moving online. A simple online rental system can make it easier for users to rent items and provide businesses with a more efficient management platform. This article explains how to build a basic online rental system using PHP from scratch.

Database Design

The first step is designing a database to store system data, including user accounts, rental items, and orders. Below is a simple structure for each table:

Users table (users): Stores user account information such as username, password, and email.

CREATE TABLE users (
  id INT(11) PRIMARY KEY AUTO_INCREMENT,
  username VARCHAR(50) NOT NULL,
  password VARCHAR(255) NOT NULL,
  email VARCHAR(100) NOT NULL,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

Items table (items): Stores details about rentable items such as name and description.

CREATE TABLE items (
  id INT(11) PRIMARY KEY AUTO_INCREMENT,
  name VARCHAR(100) NOT NULL,
  description TEXT NOT NULL,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

Orders table (orders): Records each rental transaction, including the user, item, and rental period.

CREATE TABLE orders (
  id INT(11) PRIMARY KEY AUTO_INCREMENT,
  user_id INT(11) NOT NULL,
  item_id INT(11) NOT NULL,
  start_date DATE NOT NULL,
  end_date DATE NOT NULL,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (user_id) REFERENCES users(id),
  FOREIGN KEY (item_id) REFERENCES items(id)
);

System File Structure

Create the following structure in the project’s root directory to organize files efficiently:

  • index.php: Homepage for displaying available items and rental options.
  • login.php: Login page for user authentication.
  • register.php: Registration page for creating new accounts.
  • logout.php: Logout page for destroying user sessions.
  • config.php: Database configuration file.
  • templates/: Folder containing header, footer, and item display templates.

Developing the Homepage (index.php)

The homepage displays a list of available items and adjusts its layout based on the user's login status. The following PHP code handles item retrieval and display:

<?php
// Fetch item list
$query = "SELECT * FROM items";
$result = mysqli_query($con, $query);

// Loop through and display items
while ($row = mysqli_fetch_assoc($result)) {
    echo "<div class='item'>";
    echo "<h3>" . $row['name'] . "</h3>";
    echo "<p>" . $row['description'] . "</p>";
    echo "<a href='rent.php?id=" . $row['id'] . "'>Rent</a>";
    echo "</div>";
}
?>

This code queries the database for items and dynamically generates the HTML structure for each one.

Developing the Rental Page (rent.php)

The rental page handles user rental requests. It checks whether the user is logged in; if not, it redirects them to the login page. Logged-in users can view item details, choose rental dates, and submit rental requests.

Register and Login Pages

The register and login pages handle user authentication. The registration form stores new user data, while the login form validates existing users by checking their credentials in the database. When authentication succeeds, session data is stored to maintain login state.

Logout Functionality

When users log out, their session should be destroyed, and they should be redirected to the login page. The following code handles this:

<?php
// Logout user
session_start();
session_destroy();

// Redirect to login page
header("Location: login.php");
exit;
?>

Conclusion

This tutorial demonstrated how to create a basic online rental system using PHP, including database design, user authentication, item display, and rental functionality. While this example is simplified, it serves as a strong foundation for expanding features such as payment integration, rental status tracking, and enhanced security. Hopefully, this guide helps you better understand how to apply PHP in practical web development.