当前位置:首页 > PHP

php实现文章

2026-04-02 22:06:53PHP

PHP实现文章管理功能

数据库设计

创建articles表存储文章数据,包含id、title、content、author、created_at等字段。使用MySQLi或PDO扩展连接数据库。

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

连接数据库

建立数据库连接配置文件config.php,包含数据库连接参数和PDO实例。

<?php
$host = 'localhost';
$dbname = 'article_db';
$username = 'root';
$password = '';

try {
    $pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
    die("Connection failed: " . $e->getMessage());
}
?>

文章创建功能

创建表单提交文章数据,后端处理并存入数据库。

// 处理表单提交
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $title = $_POST['title'];
    $content = $_POST['content'];
    $author = $_POST['author'];

    $stmt = $pdo->prepare("INSERT INTO articles (title, content, author) VALUES (?, ?, ?)");
    $stmt->execute([$title, $content, $author]);

    header("Location: index.php");
    exit;
}

文章列表展示

从数据库查询文章数据并以列表形式展示。

$stmt = $pdo->query("SELECT * FROM articles ORDER BY created_at DESC");
$articles = $stmt->fetchAll(PDO::FETCH_ASSOC);

foreach ($articles as $article) {
    echo "<h3>{$article['title']}</h3>";
    echo "<p>{$article['content']}</p>";
    echo "<small>作者: {$article['author']} | 发布时间: {$article['created_at']}</small>";
}

文章编辑功能

提供编辑表单并更新数据库记录。

// 获取要编辑的文章
$id = $_GET['id'];
$stmt = $pdo->prepare("SELECT * FROM articles WHERE id = ?");
$stmt->execute([$id]);
$article = $stmt->fetch();

// 更新文章
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $title = $_POST['title'];
    $content = $_POST['content'];

    $stmt = $pdo->prepare("UPDATE articles SET title = ?, content = ? WHERE id = ?");
    $stmt->execute([$title, $content, $id]);

    header("Location: index.php");
    exit;
}

文章删除功能

从数据库删除指定文章记录。

$id = $_GET['id'];
$stmt = $pdo->prepare("DELETE FROM articles WHERE id = ?");
$stmt->execute([$id]);

header("Location: index.php");
exit;

安全防护措施

对用户输入进行过滤和验证,防止SQL注入和XSS攻击。

// 过滤输入
$title = filter_input(INPUT_POST, 'title', FILTER_SANITIZE_STRING);
$content = filter_input(INPUT_POST, 'content', FILTER_SANITIZE_STRING);

// 预处理语句防止SQL注入
$stmt = $pdo->prepare("INSERT INTO articles (title, content) VALUES (:title, :content)");
$stmt->bindParam(':title', $title);
$stmt->bindParam(':content', $content);
$stmt->execute();

// 输出时防止XSS
echo htmlspecialchars($article['content'], ENT_QUOTES, 'UTF-8');

分页功能实现

处理大量文章时的分页显示逻辑。

php实现文章

$perPage = 5;
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$offset = ($page - 1) * $perPage;

$stmt = $pdo->query("SELECT COUNT(*) FROM articles");
$total = $stmt->fetchColumn();
$pages = ceil($total / $perPage);

$stmt = $pdo->prepare("SELECT * FROM articles ORDER BY created_at DESC LIMIT :offset, :perPage");
$stmt->bindParam(':offset', $offset, PDO::PARAM_INT);
$stmt->bindParam(':perPage', $perPage, PDO::PARAM_INT);
$stmt->execute();
$articles = $stmt->fetchAll();

以上实现包含了文章管理系统的基本功能模块,可根据实际需求进行扩展和优化。注意在生产环境中要添加更完善的安全措施和错误处理机制。

标签: 文章php
分享给朋友:

相关文章

php实现树

php实现树

PHP实现树结构的方法 在PHP中实现树结构通常可以通过递归或迭代的方式完成。以下是几种常见的实现方法: 递归实现树结构 递归是处理树结构的自然方式,尤其适用于具有未知深度的树。 class Tr…

php实现聊天

php实现聊天

实现 PHP 聊天功能的方法 使用 WebSocket 和 PHP 实现实时聊天 WebSocket 是一种全双工通信协议,适合实现实时聊天功能。PHP 可以通过 Ratchet 库实现 WebSo…

php实现上传图片

php实现上传图片

上传图片的基本流程 PHP 实现图片上传功能需要处理文件接收、验证、保存等步骤。以下是具体实现方法。 创建 HTML 表单 在 HTML 中创建一个表单,设置 enctype="multipart/…

php无限分类的实现

php无限分类的实现

PHP无限分类的实现 无限分类是一种常见的树形结构数据组织方式,适用于商品分类、菜单管理、地区选择等场景。以下是几种实现方法: 递归实现法 递归是最直观的实现方式,通过函数自身调用来遍历子分类。数据…

php实现博客

php实现博客

使用PHP实现博客系统 数据库设计 创建数据库表存储博客文章和用户信息。常见的表包括posts(文章)和users(用户)。 CREATE TABLE users ( id INT AUTO…

php 实现跳转

php 实现跳转

PHP 实现页面跳转的方法 在PHP中,实现页面跳转可以通过多种方式完成,以下是几种常用的方法: 使用header函数 header函数是PHP中最常用的跳转方法,通过发送HTTP头部信息实现跳转。…