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);
}
?>