A CMS (Content Management System) is a tool that helps users easily create, edit, and manage website content. This guide will walk you through building a simple and practical CMS system using PHP, enabling easy content management for your website.
First, create a database in MySQL and design a table to store article content.
CREATE DATABASE cms;
<p>USE cms;</p>
<p>CREATE TABLE articles (<br>
id INT AUTO_INCREMENT PRIMARY KEY,<br>
title VARCHAR(255) NOT NULL,<br>
content TEXT NOT NULL,<br>
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP<br>
);<br>
This table contains fields for the article's unique ID, title, content, and creation time — enough to meet basic content storage needs.
Use PHP’s mysqli class to connect to the database, ensuring that subsequent operations work smoothly.
<?php
$host = 'localhost';
$username = 'your_username';
$password = 'your_password';
$database = 'cms';
<p>$conn = new mysqli($host, $username, $password, $database);</p>
<p>if ($conn->connect_error) {<br>
die("Connection failed: " . $conn->connect_error);<br>
}</p>
<p>$conn->set_charset('utf8');<br>
?><br>
Please replace your_username and your_password with your own database credentials.
Create a page that shows all articles and provides a form for adding new articles.
<?php
$sql = "SELECT * FROM articles";
$result = $conn->query($sql);
?>
<p><!DOCTYPE html><br>
<html><br>
<head><br>
<title>CMS</title><br>
</head><br>
<body><br>
<h1>Article List</h1></p>
<p><ul><br>
<?php while($row = $result->fetch_assoc()): ?><br>
<li><br>
<h2><?php echo $row['title']; ?></h2><br>
<p><?php echo $row['content']; ?></p><br>
<p><?php echo $row['created_at']; ?></p><br>
</li><br>
<?php endwhile; ?><br>
</ul></p>
<p><h2>Add Article</h2></p>
<p><form action="add_article.php" method="POST"><br>
<input type="text" name="title" placeholder="Title"><br>
<textarea name="content" placeholder="Content"></textarea><br>
<button type="submit">Submit</button><br>
</form><br>
</body><br>
</html><br>
This code fetches all articles from the database and loops through them to display the title, content, and creation time. The form submits new article data to add_article.php.
Create add_article.php to receive the form data and insert it into the database.
<?php
$title = $_POST['title'];
$content = $_POST['content'];
<p>$sql = "INSERT INTO articles (title, content) VALUES ('$title', '$content')";</p>
<p>if ($conn->query($sql) === TRUE) {<br>
echo "Article added successfully";<br>
} else {<br>
echo "Error: " . $conn->error;<br>
}<br>
?></p>
<p><a href="index.php">Return</a><br>
This script gets the title and content from the POST request, inserts them into the database, and provides success or error messages along with a link to return to the article list.
With this tutorial, you now know how to quickly build a basic CMS system using PHP and MySQL that supports adding and displaying content. This simple system is easy to expand and can serve as a foundation for developing more advanced content management systems.