Autocodewizard Logo Practical Project - Basic CMS - Autocodewizard Ebooks - PHP Essentials: Building Dynamic Web Applications

Chapter 12: Practical Project - Basic CMS

Apply your skills to create a simple content management system (CMS) in PHP that allows users to manage content dynamically.

In this chapter, we’ll use the concepts we’ve covered so far to build a basic content management system (CMS) in PHP. This CMS will allow users to create, read, update, and delete content entries dynamically, simulating the functionality of a blog or article management system.

Setting Up the Database

Start by creating a MySQL database and a table to store the CMS content. The table might include fields like id, title, content, and created_at:

<?php
$sql = "CREATE TABLE posts (
    id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    title VARCHAR(255) NOT NULL,
    content TEXT NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)";
$conn->query($sql);
?>

This table stores each post's title, content, and creation date.

Creating the CMS Interface

Build a simple HTML interface with forms to allow users to create and edit posts, as well as view and delete them. Here’s a basic form for creating new posts:

<form action="create_post.php" method="post">
    <label for="title">Title:</label>
    <input type="text" id="title" name="title" required><br>

    <label for="content">Content:</label>
    <textarea id="content" name="content" required></textarea><br>

    <input type="submit" value="Create Post">
</form>

Creating a New Post

In create_post.php, use an INSERT INTO SQL statement to save the post data to the database:

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $title = $_POST['title'];
    $content = $_POST['content'];
    
    $sql = "INSERT INTO posts (title, content) VALUES ('$title', '$content')";
    if ($conn->query($sql) === TRUE) {
        echo "New post created successfully";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }
}
?>

This code retrieves the form data and inserts it into the posts table.

Displaying Posts

Create a view_posts.php page to display all posts stored in the database:

<?php
$sql = "SELECT id, title, content, created_at FROM posts";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        echo "<h2>" . $row["title"] . "</h2>";
        echo "<p>" . $row["content"] . "</p>";
        echo "<small>Posted on: " . $row["created_at"] . "</small><br>";
    }
} else {
    echo "No posts found.";
}
?>

This code retrieves and displays each post's title, content, and creation date.

Editing a Post

To edit a post, create a form that pre-fills with the post's current data. Here’s an example of an update form in edit_post.php:

<?php
$id = $_GET['id'];
$sql = "SELECT title, content FROM posts WHERE id=$id";
$result = $conn->query($sql);
$post = $result->fetch_assoc();
?>

<form action="update_post.php?id=<?php echo $id; ?>" method="post">
    <label for="title">Title:</label>
    <input type="text" id="title" name="title" value="<?php echo $post['title']; ?>" required><br>

    <label for="content">Content:</label>
    <textarea id="content" name="content" required><?php echo $post['content']; ?></textarea><br>

    <input type="submit" value="Update Post">
</form>

Updating a Post

In update_post.php, update the database record with the new data:

<?php
$id = $_GET['id'];
$title = $_POST['title'];
$content = $_POST['content'];

$sql = "UPDATE posts SET title='$title', content='$content' WHERE id=$id";
if ($conn->query($sql) === TRUE) {
    echo "Post updated successfully";
} else {
    echo "Error updating post: " . $conn->error;
}
?>

Deleting a Post

To delete a post, create a delete_post.php file that removes the specified post from the database:

<?php
$id = $_GET['id'];
$sql = "DELETE FROM posts WHERE id=$id";

if ($conn->query($sql) === TRUE) {
    echo "Post deleted successfully";
} else {
    echo "Error deleting post: " . $conn->error;
}
?>

This code deletes a post based on its id, allowing users to manage content effectively.

Summary and Next Steps

In this chapter, we built a basic content management system in PHP, covering creating, reading, updating, and deleting posts. This project ties together many PHP concepts and provides a solid foundation for building dynamic web applications. In the next chapter, we’ll explore advanced PHP techniques to further enhance your skills and understanding.