php实现文章
PHP 实现文章功能
使用 PHP 实现文章功能通常涉及数据库操作、前端展示和用户交互。以下是实现文章功能的关键步骤。
数据库设计
创建数据库表存储文章信息,表结构可以包含以下字段:
id:文章唯一标识,主键自增title:文章标题content:文章内容author:作者信息created_at:创建时间updated_at:更新时间
SQL 示例:
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,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
连接数据库
使用 PHP 的 PDO 或 mysqli 扩展连接数据库:
$host = 'localhost';
$dbname = 'database_name';
$username = 'username';
$password = 'password';
try {
$pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
die("Database 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]);
}
文章列表展示
从数据库查询文章并展示:
$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 获取单篇文章:
$articleId = $_GET['id'];
$stmt = $pdo->prepare("SELECT * FROM articles WHERE id = ?");
$stmt->execute([$articleId]);
$article = $stmt->fetch(PDO::FETCH_ASSOC);
if ($article) {
echo "<h1>{$article['title']}</h1>";
echo "<div>{$article['content']}</div>";
} else {
echo "文章不存在";
}
文章编辑功能
实现文章编辑和更新:
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$id = $_POST['id'];
$title = $_POST['title'];
$content = $_POST['content'];
$stmt = $pdo->prepare("UPDATE articles SET title = ?, content = ? WHERE id = ?");
$stmt->execute([$title, $content, $id]);
}
$articleId = $_GET['id'];
$stmt = $pdo->prepare("SELECT * FROM articles WHERE id = ?");
$stmt->execute([$articleId]);
$article = $stmt->fetch(PDO::FETCH_ASSOC);
文章删除功能
实现文章删除:
if (isset($_GET['delete_id'])) {
$deleteId = $_GET['delete_id'];
$stmt = $pdo->prepare("DELETE FROM articles WHERE id = ?");
$stmt->execute([$deleteId]);
}
安全防护
实现基本的安全防护措施:
- 使用预处理语句防止 SQL 注入
- 对用户输入进行过滤和验证
- 实现 CSRF 防护
- 对输出内容进行转义
function sanitizeInput($data) {
return htmlspecialchars(strip_tags(trim($data)));
}
分页功能
实现文章列表分页:
$perPage = 10;
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$offset = ($page - 1) * $perPage;
$total = $pdo->query("SELECT COUNT(*) FROM articles")->fetchColumn();
$totalPages = ceil($total / $perPage);
$stmt = $pdo->prepare("SELECT * FROM articles ORDER BY created_at DESC LIMIT ? OFFSET ?");
$stmt->bindValue(1, $perPage, PDO::PARAM_INT);
$stmt->bindValue(2, $offset, PDO::PARAM_INT);
$stmt->execute();
$articles = $stmt->fetchAll(PDO::FETCH_ASSOC);
搜索功能
实现文章搜索:
if (isset($_GET['search'])) {
$searchTerm = '%' . $_GET['search'] . '%';
$stmt = $pdo->prepare("SELECT * FROM articles WHERE title LIKE ? OR content LIKE ?");
$stmt->execute([$searchTerm, $searchTerm]);
$articles = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
以上代码示例展示了如何使用 PHP 实现基本的文章功能,包括创建、读取、更新和删除(CRUD)操作,以及分页和搜索功能。实际应用中可能需要根据具体需求进行调整和完善。







