A Content Management System (CMS) allows users to create, manage, and modify website content without needing specialized technical knowledge. While popular CMS platforms like WordPress and Joomla dominate the market, building a custom CMS tailored to your specific needs can offer greater flexibility and control over your website’s features and design.
In this guide, we’ll walk you through the process of building a basic custom CMS using PHP and MySQL. You’ll learn how to set up a database, create the necessary functionality to add, edit, and delete content, and structure your PHP files for better organization.
Prerequisites:
- Basic knowledge of PHP and MySQL
- A web server with PHP and MySQL support (XAMPP, WAMP, or MAMP for local development)
- Basic knowledge of HTML and CSS
Step 1: Setting Up the MySQL Database
First, we need to create a MySQL database to store the content for the CMS. You can use phpMyAdmin (a web interface for managing MySQL databases) or MySQL command line tools to create the database and its required tables.
1.1 Create a Database
CREATE DATABASE custom_cms;
1.2 Create a Table for Pages
In your custom_cms database, create a table called pages
to store the title and content of each page.
CREATE TABLE pages (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
content TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
Step 2: Connecting to the Database in PHP
Create a config.php file to store the database connection details. This file will be included in all PHP files that interact with the database.
<?php
$host = 'localhost';
$dbname = 'custom_cms';
$username = 'root'; // Adjust as necessary
$password = ''; // Adjust as necessary
try {
$pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
?>
Step 3: Creating the Admin Panel
The admin panel is where you’ll manage content (create, edit, delete pages). Start by creating a basic form to add new pages.
3.1 Creating the Add Page Form (add_page.php)
<?php include 'config.php'; ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Add New Page</title>
</head>
<body>
<h1>Add New Page</h1>
<form action="insert_page.php" method="post">
<label for="title">Page Title:</label><br>
<input type="text" id="title" name="title" required><br><br>
<label for="content">Content:</label><br>
<textarea id="content" name="content" rows="10" cols="50" required></textarea><br><br>
<input type="submit" value="Add Page">
</form>
</body>
</html>
3.2 Handling the Form Submission (insert_page.php)
This script inserts the submitted data from the form into the MySQL database.
<?php
include 'config.php';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$title = $_POST['title'];
$content = $_POST['content'];
$sql = "INSERT INTO pages (title, content) VALUES (:title, :content)";
$stmt = $pdo->prepare($sql);
$stmt->execute(['title' => $title, 'content' => $content]);
echo "New page added successfully!";
}
?>
Step 4: Displaying Pages on the Website
To display the content stored in the database on your website, create a pages.php file. This script will retrieve the data from the database and render it in the browser.
<?php include 'config.php'; ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CMS Pages</title>
</head>
<body>
<h1>Pages</h1>
<?php
$sql = 'SELECT * FROM pages ORDER BY created_at DESC';
$stmt = $pdo->query($sql);
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo '<h2>' . htmlspecialchars($row['title']) . '</h2>';
echo '<p>' . htmlspecialchars($row['content']) . '</p>';
echo '<hr>';
}
?>
</body>
</html>
Step 5: Editing and Deleting Pages
To allow editing and deleting of pages, you’ll need to create the respective functionalities.
5.1 Editing a Page
- edit_page.php:
<?php
include 'config.php';
$id = $_GET['id'];
$sql = "SELECT * FROM pages WHERE id = :id";
$stmt = $pdo->prepare($sql);
$stmt->execute(['id' => $id]);
$page = $stmt->fetch(PDO::FETCH_ASSOC);
?>
<form action="update_page.php" method="post">
<input type="hidden" name="id" value="<?php echo $page['id']; ?>">
<label for="title">Page Title:</label><br>
<input type="text" id="title" name="title" value="<?php echo htmlspecialchars($page['title']); ?>"><br><br>
<label for="content">Content:</label><br>
<textarea id="content" name="content" rows="10" cols="50"><?php echo htmlspecialchars($page['content']); ?></textarea><br><br>
<input type="submit" value="Update Page">
</form>
- update_page.php:
<?php
include 'config.php';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$id = $_POST['id'];
$title = $_POST['title'];
$content = $_POST['content'];
$sql = "UPDATE pages SET title = :title, content = :content WHERE id = :id";
$stmt = $pdo->prepare($sql);
$stmt->execute(['title' => $title, 'content' => $content, 'id' => $id]);
echo "Page updated successfully!";
}
?>
5.2 Deleting a Page (delete_page.php)
<?php
include 'config.php';
$id = $_GET['id'];
$sql = "DELETE FROM pages WHERE id = :id";
$stmt = $pdo->prepare($sql);
$stmt->execute(['id' => $id]);
echo "Page deleted successfully!";
?>
Step 6: Styling the CMS
While this guide focuses primarily on functionality, styling your CMS is equally important. You can use CSS or Bootstrap to style your forms, admin panel, and front-end pages. Make sure to implement responsive design techniques to ensure your CMS is accessible on all devices.
Step 7: Securing the CMS
Security is a crucial aspect of any CMS. Here are a few steps to make your CMS more secure:
- Input validation: Sanitize all user inputs to prevent SQL injection.
- Authentication: Implement a login system to restrict access to the admin panel.
- Prepared statements: Use prepared statements (as shown above) to protect against SQL injection.
Conclusion
Building a custom CMS from scratch using PHP and MySQL provides flexibility and control over how your website content is managed. This tutorial covered the basics, but you can extend the CMS by adding features like user authentication, file uploads, and more advanced content editing capabilities. Customizing a CMS allows you to create a tailored solution that meets the unique needs of your project.
For expert custom web development services, visit TechsterTech and get tailored solutions for your business!