Create a Small CRM using PHP and MySQL
Last Updated :
24 Apr, 2025
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 company. The system includes a date-filtering feature for sorting contacts.
Approach
- Define Database Requirements, which will identify the data your CRM needs to store, such as contacts.
- Database Design that designs the database schema with tables like contacts (fields: id, name, email, phone, company, created_at).
- Set Up MySQL Database, and create a new database (e.g., crm_db) using a tool like phpMyAdmin.
- Create Tables, and use SQL to create tables.
- Create db_connection.php to connect to the MySQL database
Here, we need to demonstrate the creation of a simple Customer Relationship Management (CRM) system using PHP and MySQL. The goal is to build a basic application where users can register, log in, and manage contacts.
Steps to create and configure the Project
Step 1: Set Up XAMPP Server
- Download and install XAMPP from https://www.apachefriends.org/index.html.
- Open XAMPP Control Panel and start Apache and MySQL services.
Step 2: Create Project Directory
- Navigate to the htdocs folder in the XAMPP directory.
- Create a folder named 'project1'. This folder will contain all project files.
Step 3: Database Configuration
- Go to localhost/phpMyAdmin and create a new database named 'crm_db'.
- Execute the following SQL queries to create the necessary tables:
-- Contacts Table
CREATE TABLE contacts (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(100) NOT NULL,
phone VARCHAR(15),
company VARCHAR(100),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Users Table
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL,
password VARCHAR(255) NOT NULL
);
Step 4: Insert dummy data into the tables using the provided queries.
-- Contacts Table
INSERT INTO contacts (name, email, phone, company) VALUES
('John Doe', '[email protected]', '123-456-7890', 'ABC Inc.'),
('Jane Smith', '[email protected]', '987-654-3210', 'XYZ Corp.'),
('Alice Johnson', '[email protected]', '555-123-4567', '123 Company'),
('Bob Williams', '[email protected]', '444-888-9999', 'Tech Solutions');
-- User Table
INSERT INTO users (username, password) VALUES
('user1', '$2y$10$YqJDXBGf57s5Uz7INveu6uTbfXvdf4NzjXEEDp5j86f/h9kGj.4uK'),
('user2', '$2y$10$R4eBLPpZ4E8a0ZG8lxMQVOP7NCCf8ww0PQ7jDy/FwOZ2jhksKbU1u'),
('user3', '$2y$10$5/xgKedP/uJbPzdCN/TI2.GgMz5d2PhGUV1TLE8L3G5IR6veK5n3i'),
('user4', '$2y$10$ap6T9AZm5pumRx/8D9/x7uRUJ01sM/G9Wj2Opgk7jbjFWkWXpVXx2');
Step 5: Inside the 'project1' folder, create the following files. Establishes a connection to the MySQL database using the provided host, username, password, and database name. Checks for a successful connection, and if an error occurs, it terminates the script and displays an error message.
PHP
// db_connection.php
<?php
$host = "localhost";
$username = "your_username";
$password = "your_password";
$database = "crm_db";
$conn = new mysqli($host, $username, $password, $database);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
Step 6: Define two essential functions for user authentication: registerUser and loginUser. The registerUser inserts a new user into the 'users' table with a hashed password. The loginUser retrieves a user from the 'users' table, compares the provided password with the hashed password, and returns a boolean indicating successful login.
PHP
// auth_functions.php
<?php
function registerUser($username, $password)
{
global $conn;
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);
$sql = "INSERT INTO users (username, password)
VALUES ('$username', '$hashedPassword')";
return $conn->query($sql);
}
function loginUser($username, $password)
{
global $conn;
$sql = "SELECT * FROM users WHERE username = '$username'";
$result = $conn->query($sql);
if ($result->num_rows == 1) {
$user = $result->fetch_assoc();
if (password_verify($password, $user["password"])) {
return true;
}
}
return false;
}
?>
Step 7: Includes the necessary files (db_connection.php and auth_functions.php) for database connection and user authentication functions. Processes the user registration form when submitted, capturing the username and password. Calls the registerUser function to insert the new user into the 'users' table. Displays a success or failure message based on the registration outcome.
PHP
// register.php
<?php
include "db_connection.php";
include "auth_functions.php";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = $_POST["username"];
$password = $_POST["password"];
if (registerUser($username, $password)) {
echo '<p style="color: green;">Registration successful.'.
' <a href="login.php">Login</a></p>';
} else {
echo '<p style="color: red;">Registration failed.</p>';
}
}
?>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 0;
}
form {
max-width: 400px;
margin: 20px auto;
padding: 20px;
background-color: #fff;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
form input {
width: 100%;
padding: 10px;
margin-bottom: 10px;
box-sizing: border-box;
}
form input[type="submit"] {
background-color: #4caf50;
color: #fff;
cursor: pointer;
}
form input[type="submit"]:hover {
background-color: #45a049;
}
</style>
<form method="post" action="">
<h1>Register Now!!!</h1>
<label for="username">Username:</label>
<input type="text" name="username" required><br>
<label for="password">Password:</label>
<input type="password" name="password" required><br>
<input type="submit" value="Register">
</form>
?> ?>
Register page looks like given image.

Step 8: Includes the required files (db_connection.php and auth_functions.php) for database connection and user authentication functions. Initiates a session and processes the login form when submitted, capturing the username and password. Calls the loginUser function to authenticate the user against the stored credentials in the 'users' table. Redirects the user to contacts.php upon successful login or displays a login failure message.
PHP
// Login.php
<?php
include "db_connection.php";
include "auth_functions.php";
session_start();
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = $_POST["username"];
$password = $_POST["password"];
if (loginUser($username, $password)) {
$_SESSION["username"] = $username;
header("Location: contacts.php");
exit();
} else {
echo '<p style="color: red;">Login failed.</p>';
}
}
?>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 0;
}
form {
max-width: 400px;
margin: 20px auto;
padding: 20px;
background-color: #fff;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
form input {
width: 100%;
padding: 10px;
margin-bottom: 10px;
box-sizing: border-box;
}
form input[type="submit"] {
background-color: #4caf50;
color: #fff;
cursor: pointer;
}
form input[type="submit"]:hover {
background-color: #45a049;
}
</style>
<form method="post" action="">
<h1>Login</h1>
<label for="username">Username:</label>
<input type="text" name="username" required><br>
<label for="password">Password:</label>
<input type="password" name="password" required><br>
<input type="submit" value="Login">
</form>
Login page looks like given image.

Step 9: Includes the db_connection.php file to establish a connection to the MySQL database. Initiates a session and checks if the user is authenticated; otherwise, it redirects to the login page. Retrieves the username from the session and displays a personalized welcome message. Queries the 'contacts' table to fetch contact information and dynamically generates an HTML table to display the contacts. Applies inline styles to enhance the visual presentation of the page, including font styles, background colors, and table formatting.
PHP
// contacts.php
<?php
include "db_connection.php";
session_start();
if (!isset($_SESSION["username"])) {
header("Location: login.php");
exit();
}
$username = $_SESSION["username"];
// Fetch contacts from the database
$result = $conn->query("SELECT * FROM contacts");
// Inline styles for a simple table
echo '<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 0;
}
h1, h2 {
color: #333;
}
table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}
table, th, td {
border: 1px solid #ddd;
}
th, td {
padding: 12px;
text-align: left;
}
th {
background-color: #4caf50;
color: #fff;
}
a {
color: #007bff;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
</style>';
// Display contacts in a table
echo "<h1>Welcome, " . $username . "!</h1>";
echo "<h2>Your Contacts:</h2>";
echo "<table>";
echo "<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Phone</th>
<th>Company</th>
<th>Created At</th>
</tr>";
while ($row = $result->fetch_assoc()) {
echo "<tr>
<td>{$row["id"]}</td>
<td>{$row["name"]}</td>
<td>{$row["email"]}</td>
<td>{$row["phone"]}</td>
<td>{$row["company"]}</td>
<td>{$row["created_at"]}</td>
</tr>";
}
echo "</table>";
?>
phpMyAdmin Database:

Output:
.gif)
Similar Reads
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 an activate/deactivate button using PHP and MySQL
In this article, we will discuss how to create an Activate/Deactivate button using PHP. When a particular status is set, the same gets updated in the database. Approach: In order to illustrate the example, let's say that there are a few courses that can be set as active or inactive and need to have
4 min read
Build a Grocery Store Web App using PHP with MySQL
In this article, we are going to build a Grocery Store Web Application using PHP with MySQL. In this application, we can add grocery items by their name, quantity, status (pending, bought, not available), and date. We can view, delete and update those items. There will be a date filtering feature wh
7 min read
How to retrieve data from MySQL database using PHP ?
There are steps to understand for retrieving the data from the MySQL database. Approach: Create the database, then create the table for data.Enter the rows in the table. You have to connect to the database. Now we understand each and every step as shown below.  Example 1: In this. we use PHPMyAdmin
2 min read
How to Create a MySQL REST API
Creating a REST API is important for enabling communication between different software systems. MySQL is one of the most popular relational database management systems which serves as the backbone for data storage in web applications. In this article, we will learn how to create a REST API using MyS
6 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
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
5 min read
PHP | MySQL ( Creating Table )
What is a table? In relational databases, and flat file databases, a table is a set of data elements using a model of vertical columns and horizontal rows, the cell being the unit where a row and column intersect. A table has a specified number of columns, but can have any number of rows. Creating a
3 min read
How to Update Data in MySQL Database Table Using PHP?
Updating data in a MySQL database table using PHP is a fundamental aspect of web development, particularly in applications where user interactions involve modifying existing records. This guide delves into the process of updating data in a MySQL database table using PHP, covering database connection
3 min read
Create a Database Using AWS rds
Make a database on cloud platform using AWS (Amazon Web Services). Introduction to Cloud Cloud computing is an information technology paradigm that enables ubiquitous access to shared pools of configurable system resources and higher-level services that can be rapidly provisioned with minimal manage
5 min read