Create a Small CRM using PHP and MySQL
Last Updated :
28 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
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
What is Vacuum Circuit Breaker? A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac
13 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
CTE in SQL In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi
6 min read
Python Variables In Python, variables are used to store data that can be referenced and manipulated during program execution. A variable is essentially a name that is assigned to a value. Unlike many other programming languages, Python variables do not require explicit declaration of type. The type of the variable i
6 min read
Spring Boot Interview Questions and Answers Spring Boot is a Java-based framework used to develop stand-alone, production-ready applications with minimal configuration. Introduced by Pivotal in 2014, it simplifies the development of Spring applications by offering embedded servers, auto-configuration, and fast startup. Many top companies, inc
15+ min read