0
点赞
收藏
分享

微信扫一扫

6. 论坛管理系统CMS

潇湘落木life 2024-11-21 阅读 17

代码:

// db.php - 数据库连接
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "forum_cms";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// create_post.php - 创建新帖子
if(isset($_POST['create_post'])){
    $title = $_POST['title'];
    $content = $_POST['content'];
    $category = $_POST['category'];
    
    $sql = "INSERT INTO posts (title, content, category) VALUES ('$title', '$content', '$category')";
    if(mysqli_query($conn, $sql)){
        echo "Post created successfully!";
    } else {
        echo "Error: " . $sql . "<br>" . mysqli_error($conn);
    }
}

// show_posts.php - 展示所有帖子
$query = "SELECT * FROM posts ORDER BY created_at DESC";
$result = mysqli_query($conn, $query);
while($row = mysqli_fetch_assoc($result)) {
    echo "<h2>" . $row['title'] . "</h2>";
    echo "<p>" . $row['content'] . "</p>";
    echo "<p>Category: " . $row['category'] . "</p>";
}

// add_comment.php - 添加评论
if(isset($_POST['add_comment'])){
    $post_id = $_POST['post_id'];
    $comment = $_POST['comment'];
    
    $sql = "INSERT INTO comments (post_id, comment) VALUES ('$post_id', '$comment')";
    if(mysqli_query($conn, $sql)){
        echo "Comment added!";
    } else {
        echo "Error: " . $sql . "<br>" . mysqli_error($conn);
    }
}

// show_comments.php - 展示评论
$post_id = $_GET['post_id'];
$query = "SELECT * FROM comments WHERE post_id = '$post_id'";
$result = mysqli_query($conn, $query);
while($row = mysqli_fetch_assoc($result)) {
    echo "<p>" . $row['comment'] . "</p>";
}

举报

相关推荐

0 条评论