How to build a simple CMS system using PHP

WBOY
Release: 2023-09-06 15:02:02
Original
1190 people have browsed it

如何使用 PHP 构建一个简单的 CMS 系统

How to use PHP to build a simple CMS system

CMS (Content Management System) refers to the content management system, which can help us easily create, edit and manage Website content. In this article, we will introduce how to use PHP to build a simple CMS system to easily manage the content of the website.

Before we begin, we need to have a basic understanding and mastery of the following concepts:

  1. PHP: A server-side scripting language used to develop dynamic websites.
  2. MySQL: A database management system used to store and manage website data.
  3. HTML, CSS and JavaScript: used to build and beautify website pages.

Next, let’s build our simple CMS system step by step.

Step One: Create Database and Table
First, we need to create a MySQL database and create a table in the database to store the content of the website.

CREATE DATABASE cms;

USE cms;

CREATE TABLE articles (
  id INT AUTO_INCREMENT PRIMARY KEY,
  title VARCHAR(255) NOT NULL,
  content TEXT NOT NULL,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
Copy after login

In the above code, we create a database named cms and create a table named articles in it. The table contains four fields: id, title, content and created_at, which are used to store the unique identifier, title, content and creation time of the article respectively.

Step 2: Connect to the database
Next, we need to connect to the database in the PHP file and establish a connection to the database.

$host = 'localhost';
$username = 'your_username';
$password = 'your_password';
$database = 'cms';

$conn = new mysqli($host, $username, $password, $database);

if ($conn->connect_error) {
  die("连接失败:" . $conn->connect_error);
}

// 设置字符集
$conn->set_charset('utf8');
Copy after login

In the above code, we use the mysqli class to connect to the database. Please replace your_username and your_password with your own database username and password.

Step 3: Create a page
Next, we need to create a page for displaying the article list and adding new articles.

<?php
  // 查询所有文章
  $sql = "SELECT * FROM articles";
  $result = $conn->query($sql);
?>

<!DOCTYPE html>
<html>
<head>
  <title>CMS</title>
</head>
<body>
  <h1>文章列表</h1>

  <ul>
    <?php while ($row = $result->fetch_assoc()): ?>
    <li>
      <h2><?php echo $row['title']; ?></h2>
      <p><?php echo $row['content']; ?></p>
      <p><?php echo $row['created_at']; ?></p>
    </li>
    <?php endwhile; ?>
  </ul>

  <h2>添加文章</h2>

  <form action="add_article.php" method="POST">
    <input type="text" name="title" placeholder="标题">
    <textarea name="content" placeholder="内容"></textarea>
    <button type="submit">提交</button>
  </form>
</body>
</html>
Copy after login

In the above code, we obtain the list of articles by querying the database, and use a loop to output the title, content and creation time of each article to the page. At the same time, we also created a form where users can enter the title and content of the new article, and the form submission action will be sent to the add_article.php file.

Step 4: Add Article
Next, we need to create a page add_article.php that handles adding new articles.

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

  $sql = "INSERT INTO articles (title, content) VALUES ('$title', '$content')";

  if ($conn->query($sql) === TRUE) {
    echo "文章添加成功";
  } else {
    echo "错误:" . $conn->error;
  }
?>

<a href="index.php">返回</a>
Copy after login

In the above code, we use $_POST to get the article title and content entered by the user in the form and insert it into the database. If the insertion is successful, we will output "Article added successfully", otherwise we will output an error message. At the same time, we also provide a link that users can click to return to the article list page.

So far, we have completed the construction of a simple CMS system. You can add and view articles through pages, realizing simple content management functions.

Summary:
By using PHP and MySQL, we can easily build a simple CMS system. This system helps us manage the content of our website, including adding, editing and displaying articles. By learning and understanding this simple CMS system, you can further extend and improve it to better suit your actual needs. I hope this article helps you build your own CMS system!

The above is the detailed content of How to build a simple CMS system using PHP. 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