PHP打造高效新闻发布系统

admin 2025-01-14 671 0

在信息爆炸的时代,新闻发布系统成为各类网站不可或缺的一部分。本文将带你一步步用PHP构建一个简单高效的新闻发布系统,助你轻松管理和发布新闻内容。

PHP打造高效新闻发布系统

项目准备

首先,确保你的开发环境已安装PHP和MySQL。你可以使用XAMPP、WAMP等集成环境快速搭建。

1. 数据库设计

在MySQL中创建一个名为news的数据库,并设计如下表结构:

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

2. 项目结构

在项目根目录下创建以下文件夹和文件:

/news_system
    /includes
        - db.php
    /admin
        - add_article.php
        - manage_articles.php
    /public
        - index.php
    - style.css

核心代码实现

1. 数据库连接

includes/db.php中配置数据库连接:

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "news";

// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);

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

2. 新闻添加功能

admin/add_article.php中实现新闻添加表单及处理逻辑:

<?php
include '../includes/db.php';

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $title = $_POST['title'];
    $content = $_POST['content'];
    $author = $_POST['author'];

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

    if ($conn->query($sql) === TRUE) {
        echo "新闻添加成功!";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }
}
?>

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" type="text/css" href="../style.css">
</head>
<body>
    <h2>添加新闻</h2>
    <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
        标题: <input type="text" name="title" required><br>
        内容: <textarea name="content" required></textarea><br>
        作者: <input type="text" name="author" required><br>
        <input type="submit" value="提交">
    </form>
</body>
</html>

3. 新闻管理功能

admin/manage_articles.php中实现新闻列表及删除功能:

<?php
include '../includes/db.php';

if (isset($_GET['delete'])) {
    $id = $_GET['delete'];
    $sql = "DELETE FROM articles WHERE id=$id";
    if ($conn->query($sql) === TRUE) {
        echo "新闻删除成功!";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }
}

$sql = "SELECT id, title, author, created_at FROM articles";
$result = $conn->query($sql);
?>

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" type="text/css" href="../style.css">
</head>
<body>
    <h2>管理新闻</h2>
    <table>
        <tr>
            <th>标题</th>
            <th>作者</th>
            <th>发布时间</th>
            <th>操作</th>
        </tr>
        <?php while($row = $result->fetch_assoc()): ?>
        <tr>
            <td><?php echo $row['title']; ?></td>
            <td><?php echo $row['author']; ?></td>
            <td><?php echo $row['created_at']; ?></td>
            <td><a href="?delete=<?php echo $row['id']; ?>">删除</a></td>
        </tr>
        <?php endwhile; ?>
    </table>
</body>
</html>

4. 前端展示

public/index.php中展示新闻列表:


<?php
include '../includes/db.php';

$sql = "SELECT id, title, content, author, created_at FROM articles ORDER BY created_at DESC";
$result = $conn->query($sql);
?>

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" type="text/css" href="../style.css">

评论(0)