
php--如何编写一个简易的论坛
发布日期:2021-05-06 21:18:46
浏览次数:25
分类:原创文章
本文共 11564 字,大约阅读时间需要 38 分钟。
include3.php(数据库连接)
function doDB(){ global $conn3; $conn3 = mysqli_connect('localhost','root','','php_project01'); if(mysqli_connect_errno()) { echo "数据库连接失败!".mysqli_connect_error()."<br>"; exit(); }}
addtopic.php(增加主题)
<html><head> <title>增加一个主题</title></head><body> <h1>增加一个主题</h1> <form method="post" action="to_addtopic.php"> <p><label for="topic_owner">你的邮件地址:</label><br> <input type="email" id="topic_owner" name="topic_owner" size="40" maxlength="150" required="required"></p> <p><label for="topic_title">主题题目:</label><br> <input type="text" name="topic_title" id="topic_title" size="40" maxlength="150" required="required"></p> <p><label for="post_text">回复内容:</label><br> <textarea id="post_text" name="post_text" rows="8" cols="40"></textarea></p> <button type="submit" name="submit" value="submit">增加主题</button> </form></body></html>
toaddtopic.php(保存主题)
<?php/** * Created by PhpStorm. */include ('include3.php');doDB();if((!$_POST['topic_owner']) || (!$_POST['topic_title']) || (!$_POST['post_text'])){ header("Location:addtopic.php"); exit;}$topic_owner = mysqli_real_escape_string($conn3,$_POST['topic_owner']);$topic_title = mysqli_real_escape_string($conn3,$_POST['topic_title']);$post_text = mysqli_real_escape_string($conn3,$_POST['post_text']);$add_topic_sql = "insert into forum_topics(topic_title,topic_create_time,topic_owner) values('$topic_title',now(),'$topic_owner')";$add_topic_result = mysqli_query($conn3,$add_topic_sql);$topic_id = mysqli_insert_id($conn3);$add_post_sql = "insert into forum_posts(topic_id,post_text,post_create_time,post_owner) values('$topic_id','$post_text',now(),'$topic_owner')";$add_post_result = mysqli_query($conn3,$add_post_sql);mysqli_close($conn3);$display_block = "<p><strong>".$_POST['topic_title']."</strong>已创建成功!</p>";?><html><head> <title>增加新主题</title></head><body> <h1>增加新主题</h1> <?php echo $display_block;?></body></html>
topiclist.php(论坛主题列表)
<?php/** * Created by PhpStorm. */include_once ('include3.php');doDB();$get_topics_sql = "select topic_id,topic_title, DATE_FORMAT(topic_create_time,'%b %e %Y at %r') AS fmt_topic_create_time,topic_owner from forum_topics order by topic_create_time desc";$get_topics_res = mysqli_query($conn3,$get_topics_sql) or die(mysqli_error($conn3));if(mysqli_num_rows($get_topics_res) < 1){ $display_block = "<p><strong>没有相应的主题存在!</strong></p>";}else{ $display_block = <<< END_OF_TEXT <table> <tr> <th>主题题目</th> <th>回复数</th> </tr>END_OF_TEXT; while($topic_info = mysqli_fetch_array($get_topics_res)) { $topic_id = $topic_info['topic_id']; $topic_title = stripslashes($topic_info['topic_title']); $topic_create_time = $topic_info['fmt_topic_create_time']; $topic_owner = stripslashes($topic_info['topic_owner']); $get_num_posts_sql = "select count(post_id) as post_count from forum_posts where topic_id=$topic_id"; $get_num_posts_res = mysqli_query($conn3,$get_num_posts_sql) or die(mysqli_error($conn3)); while($posts_info = mysqli_fetch_array($get_num_posts_res)) { $num_posts = $posts_info['post_count']; } $display_block .= <<< END_OF_TEXT <tr> <td><a href="showtopic.php?topic_id=$topic_id"><strong>$topic_title</strong></a><br> 由 $topic_owner 于 $topic_create_time 创建的。</td> <td class="num_posts_col">$num_posts</td> </tr>END_OF_TEXT; } mysqli_free_result($get_topics_res); mysqli_free_result($get_num_posts_res); mysqli_close($conn3); $display_block .= "</table>";}?><html><head> <title>简易论坛</title> <style type="text/css"> table { border: 1px solid black; border-collapse: collapse; } th { border: 1px solid black; padding: 6px; font-weight: bold; background-color: #cccccc; } td { border: 1px solid black; padding: 6px; } .num_posts_col { text-align: center; } </style></head><body> <h1>简易论坛</h1> <?php echo $display_block;?> <p>你也可以<a href="addtopic.php">新建一个主题</a>!</p></body></html>
showtopic.php(显示帖子)
<?php/** * Created by PhpStorm. */include ('include3.php');doDB();if(!isset($_GET['topic_id'])){ header("location:topiclist.php"); exit;}$topic_id = mysqli_real_escape_string($conn3,$_GET['topic_id']);$topic_sql = "select topic_title from forum_topics where topic_id = $topic_id";$topic_res = mysqli_query($conn3,$topic_sql) or die(mysqli_error($conn3));if(mysqli_num_rows($topic_res) < 1){ $display_block = "<p><strong>你选择的主题题目已不存在,请<a href='topiclist.php'>重新选择</a>!</strong></p>";}else{ while($topic_info = mysqli_fetch_array($topic_res)) { $topic_title = stripslashes($topic_info['topic_title']); } $get_posts_sql = "select post_id,post_text, DATE_FORMAT(post_create_time,'%b %e %Y %r') as fmt_post_create_time, post_owner from forum_posts where topic_id=$topic_id order by post_create_time asc"; $get_post_res = mysqli_query($conn3,$get_posts_sql) or die(mysqli_error($conn3)); $display_block = <<< END_OF_TEXT <p>关于<stron>[$topic_title]</stron>的相关回复内容如下:</p> <table > <tr> <th>回复</th> <th>内容</th> </tr> END_OF_TEXT; while($posts_info = mysqli_fetch_array($get_post_res)) { $post_id = $posts_info['post_id']; $post_text =nl2br(stripslashes($posts_info['post_text'])); $post_create_time = $posts_info['fmt_post_create_time']; $post_owner = stripslashes($posts_info['post_owner']); $display_block .= <<< END_TEXT <tr> <td>回复人:$post_owner<br><br> 创建时间:$post_create_time</td> <td>$post_text<br><br> <a href="replytopost.php?post_id=$post_id"><strong>回复该帖</strong></a></td> </tr>END_TEXT; } mysqli_free_result($get_post_res); mysqli_free_result($topic_res); mysqli_close($conn3); $display_block .= "</table>";}?><html><head> <title>查看帖子</title> <style type="text/css"> table { border: 1px solid black; border-collapse: collapse; } th { border: 1px solid black; padding: 6px; font-weight: bold; background-color: #cccccc; } td { border: 1px solid black; padding: 6px; vertical-align: top; } </style></head><body> <h1>查看帖子</h1> <?php echo $display_block;?></body></html>
replytopost.php(回复帖子)
<?php/** * Created by PhpStorm. */include ('include3.php');doDB();if(!$_POST){ if (!isset($_GET['post_id'])) { header("location:topiclist.php"); exit; } $post_id = mysqli_real_escape_string($conn3, $_GET['post_id']); $sql = "select ft.topic_id,ft.topic_title from forum_posts as fp LEFT JOIN forum_topics as ft ON fp.topic_id = ft.topic_id where fp.post_id=$post_id"; $res = mysqli_query($conn3, $sql); if (mysqli_num_rows($res) < 1) { //header("location:topiclist.php"); exit; } else { while ($topic_info = mysqli_fetch_array($res)) { $topic_id = $topic_info['topic_id']; $topic_title = stripslashes($topic_info['topic_title']); } ?> <html> <head> <title>回复帖子</title> </head> <body> <h1>回复[<?php echo $topic_title; ?>]的帖子。</h1> <form method="post" action="<?php $_SERVER['PHP_SELF'] ?>"> <p><label for="post_owner">邮箱地址:</label> <input type="email" id="post_owner" name="post_owner" size="40" maxlength="150" required="required"></p> <p><label>回复内容:</label> <textarea id="post_text" name="post_text" rows="8" cols="50" required="required"></textarea></p> <input type="hidden" name="topic_id" id="topic_id" value="<?php echo $topic_id; ?>"> <button type="submit" name="submit" value="submit">提交回复</button> </form> </body> </html> <?php } mysqli_free_result($res); mysqli_close($conn3);}else if($_POST){ if((!$_POST['topic_id']) || (!$_POST['post_text']) || (!$_POST['post_owner'])) { header("location:topiclist.php"); exit; } $topic_id = mysqli_real_escape_string($conn3,$_POST['topic_id']); $post_text = mysqli_real_escape_string($conn3,$_POST['post_text']); $post_owner = mysqli_real_escape_string($conn3,$_POST['post_owner']); $add_post_sql = "insert into forum_posts(topic_id,post_text,post_create_time,post_owner) values('$topic_id','$post_text',now(),'$post_owner')"; $add_post_res = mysqli_query($conn3,$add_post_sql) or die(mysqli_error($conn3)); mysqli_close($conn3); header("location:showtopic.php?topic_id=$topic_id"); exit;}?>
数据库文件
-- phpMyAdmin SQL Dump-- version 4.8.4-- https://www.phpmyadmin.net/---- 主机: 127.0.0.1-- 生成日期: 2019-06-01 -- 服务器版本: 10.1.37-MariaDB-- PHP 版本: 7.3.1SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";SET AUTOCOMMIT = 0;START TRANSACTION;SET time_zone = "+00:00";/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;/*!40101 SET NAMES utf8mb4 */;---- 数据库: `php_project01`---- ------------------------------------------------------------ 表的结构 `forum_posts`--CREATE TABLE `forum_posts` ( `post_id` int(11) NOT NULL, `topic_id` int(11) NOT NULL, `post_text` text, `post_create_time` datetime DEFAULT NULL, `post_owner` varchar(150) DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8;---- 转存表中的数据 `forum_posts`--INSERT INTO `forum_posts` (`post_id`, `topic_id`, `post_text`, `post_create_time`, `post_owner`) VALUES(2, 2, 'I\'m interested in knowing how people got started in technology -- did you thinker with household electronics? did you learn about it in school? did you parents buy you a computer and tell you to have at it.', '2019-06-01 09:09:38', 'jane@doe.com'),(6, 4, '如题,这是一个简易的论坛,用于测试的。\r\n', '2019-06-01 17:00:46', 'abc@163.com'),(7, 4, '这是一个测试的回复。', '2019-06-01 17:17:29', 'bat@baba.com');-- ------------------------------------------------------------ 表的结构 `forum_topics`--CREATE TABLE `forum_topics` ( `topic_id` int(11) NOT NULL, `topic_title` varchar(150) DEFAULT NULL, `topic_create_time` datetime DEFAULT NULL, `topic_owner` varchar(150) DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8;---- 转存表中的数据 `forum_topics`--INSERT INTO `forum_topics` (`topic_id`, `topic_title`, `topic_create_time`, `topic_owner`) VALUES(2, 'How did you get statrted with technology?', '2019-06-01 09:09:38', 'jane@doe.com'),(4, '这是一个简易的论坛,用于测试。', '2019-06-01 17:00:46', 'abc@163.com');---- 转储表的索引------ 表的索引 `forum_posts`--ALTER TABLE `forum_posts` ADD PRIMARY KEY (`post_id`);---- 表的索引 `forum_topics`--ALTER TABLE `forum_topics` ADD PRIMARY KEY (`topic_id`);---- 在导出的表使用AUTO_INCREMENT------ 使用表AUTO_INCREMENT `forum_posts`--ALTER TABLE `forum_posts` MODIFY `post_id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=8;---- 使用表AUTO_INCREMENT `forum_topics`--ALTER TABLE `forum_topics` MODIFY `topic_id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=5;COMMIT;/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;