Home > Backend Development > PHP Tutorial > How to use PHP to implement the article draft function of CMS system

How to use PHP to implement the article draft function of CMS system

WBOY
Release: 2023-08-05 20:34:01
Original
818 people have browsed it

How to use PHP to implement the article draft function of the CMS system

With the continuous development of the Internet, the CMS (Content Management System) system has become a standard feature of many websites. In a CMS system, the draft function is a very important part. It allows administrators or authors to edit and adjust articles before publishing them to ensure the accuracy and completeness of the article content. This article will introduce how to use PHP to implement the article draft function of a simple and practical CMS system.

First, we need to create a database table to store article and draft data. We can create a table called "articles" with the following fields:

  • id: the unique identifier of the article
  • title: the title of the article
  • content: the text content of the article
  • status: the status of the article (1 means published, 0 means draft)
  • created_at: the time when the article was created
  • updated_at: the last update of the article Time

Next, we need to create a page to display the article list. When we click on the article title, we will jump to a page to display the detailed content of the article. On this page, we can add an "Edit" button to edit the article, and a "Save Draft" button to save our edits as a draft.

In the edit page, we need to add a form to receive the article title and body content entered by the user. When the user clicks the "Save Draft" button, we will use PHP to process the form data and save it as a draft. The code example is as follows:

<?php
// 连接数据库
$pdo = new PDO("mysql:host=127.0.0.1;dbname=cms_db", "root", "password");

// 获取用户输入的文章标题和正文内容
$title = $_POST['title'];
$content = $_POST['content'];

// 将草稿数据插入数据库
$stmt = $pdo->prepare("INSERT INTO articles (title, content, status, created_at, updated_at) VALUES (?, ?, 0, NOW(), NOW())");
$stmt->execute([$title, $content]);

// 提示用户成功保存草稿
echo '草稿已保存!';

// 关闭数据库连接
$pdo = null;
?>
Copy after login

In the page that displays the detailed content of the article, we can add an "Edit" button. Clicking this button will jump to the editing page, where users can edit the article and save it as a draft.

Finally, we can add a filter condition on the article list page to display all draft articles. We just need to set the "status" field in the query condition to 0. The code example is as follows:

<?php
// 连接数据库
$pdo = new PDO("mysql:host=127.0.0.1;dbname=cms_db", "root", "password");

// 查询所有草稿文章
$stmt = $pdo->prepare("SELECT * FROM articles WHERE status = 0 ORDER BY created_at DESC");
$stmt->execute();
$drafts = $stmt->fetchAll(PDO::FETCH_ASSOC);

// 显示草稿文章列表
foreach ($drafts as $draft) {
    echo '<div>';
    echo '<h2>' . $draft['title'] . '</h2>';
    echo '<p>' . $draft['content'] . '</p>';
    echo '</div>';
}

// 关闭数据库连接
$pdo = null;
?>
Copy after login

Through the above code example, we can easily implement the article draft function in the CMS system. Users can edit article content on the edit page and save it as a draft. At the same time, all draft articles can be filtered and displayed on the article list page. This will provide convenience and flexibility to administrators or authors, helping them better manage and publish articles.

The above is the detailed content of How to use PHP to implement the article draft function of CMS system. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template