当前位置:首页 > PHP

php实现文章系统

2026-02-15 18:04:21PHP

数据库设计

创建数据库表存储文章数据,常用字段包括文章ID、标题、内容、作者、发布时间等。示例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
);

连接数据库

使用PDO或MySQLi扩展连接MySQL数据库:

$host = 'localhost';
$dbname = 'article_system';
$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['author']}</p>";
    echo "<p>发布时间: {$article['created_at']}</p>";
    echo "<div>{$article['content']}</div>";
    echo "<hr>";
}

文章编辑功能

实现文章更新功能:

if (isset($_GET['edit'])) {
    $id = $_GET['edit'];
    $stmt = $pdo->prepare("SELECT * FROM articles WHERE id = ?");
    $stmt->execute([$id]);
    $article = $stmt->fetch(PDO::FETCH_ASSOC);
}

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['update'])) {
    $id = $_POST['id'];
    $title = $_POST['title'];
    $content = $_POST['content'];
    $author = $_POST['author'];

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

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

文章删除功能

实现文章删除功能:

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

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

安全防护措施

防止SQL注入和XSS攻击:

// 使用预处理语句防止SQL注入
$stmt = $pdo->prepare("SELECT * FROM articles WHERE id = ?");
$stmt->execute([$id]);

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

分页功能实现

添加文章列表分页功能:

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

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

// 获取总页数
$total = $pdo->query("SELECT COUNT(*) FROM articles")->fetchColumn();
$totalPages = ceil($total / $perPage);

文件上传功能

实现文章图片上传功能:

if (isset($_FILES['image'])) {
    $targetDir = "uploads/";
    $targetFile = $targetDir . basename($_FILES["image"]["name"]);
    $imageFileType = strtolower(pathinfo($targetFile, PATHINFO_EXTENSION));

    // 检查文件类型
    $allowedTypes = ['jpg', 'png', 'jpeg', 'gif'];
    if (in_array($imageFileType, $allowedTypes)) {
        move_uploaded_file($_FILES["image"]["tmp_name"], $targetFile);
        // 将文件路径存入数据库
        $stmt = $pdo->prepare("UPDATE articles SET image_path = ? WHERE id = ?");
        $stmt->execute([$targetFile, $articleId]);
    }
}

php实现文章系统

标签: 系统文章
分享给朋友:

相关文章

vue实现文章目录

vue实现文章目录

Vue 实现文章目录的方法 使用插件自动生成目录 安装 vue-toc 插件: npm install vue-toc --save 在组件中引入并使用: <template> &…

vue实现在线学习系统

vue实现在线学习系统

实现功能模块设计 在线学习系统通常需要包含课程展示、用户管理、学习进度跟踪、互动讨论等功能模块。使用Vue.js可以高效构建这些模块。 课程展示模块需要设计课程卡片、分类筛选、搜索功能。用户管理模…

php实现博客系统

php实现博客系统

数据库设计 创建MySQL数据库表存储博客内容。主要需要posts表(文章)、categories表(分类)、users表(用户)和comments表(评论)。以下是核心表的SQL示例: CREA…

vue实现登录系统

vue实现登录系统

Vue 登录系统实现步骤 前端部分 安装依赖 确保项目已安装 Vue 3 和必要的库(如 axios、vue-router): npm install axios vue-router 路由配置…

用vue实现会员系统

用vue实现会员系统

使用Vue实现会员系统 项目初始化与依赖安装 创建一个新的Vue项目,安装必要的依赖: vue create member-system cd member-system npm install v…

实现react事件系统

实现react事件系统

React 事件系统实现原理 React 的事件系统是合成事件(SyntheticEvent)系统,它是对原生 DOM 事件的跨浏览器包装器。React 通过事件委托机制将所有事件绑定到 docume…