如何编写一个简易的论坛--php

未结 0 2859
VIP1 2019年09月20日
悬赏:50积分
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/modern358/article/details/90731366
include3.php(数据库连接)

function doDB()
{
global $conn3;

$conn3 = mysqli_connect('localhost','root','','php_project01');
if(mysqli_connect_errno())
{
echo "数据库连接失败!".mysqli_connect_error()."
";
exit();
}
}
addtopic.php(增加主题)



增加一个主题


增加一个主题





required="required">





required="required">











toaddtopic.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 = "

".$_POST['topic_title']."已创建成功!

";
?>


增加新主题


增加新主题





topiclist.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 = "

没有相应的主题存在!

";
}
else
{
$display_block = <<< END_OF_TEXT






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




END_OF_TEXT;
}
mysqli_free_result($get_topics_res);
mysqli_free_result($get_num_posts_res);
mysqli_close($conn3);
$display_block .= "
主题题目 回复数
$topic_title

由 $topic_owner 于 $topic_create_time 创建的。
$num_posts
";
}
?>



简易论坛



简易论坛



你也可以新建一个主题!




showtopic.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 = "

你选择的主题题目已不存在,请重新选择

";
}
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

关于[$topic_title]的相关回复内容如下:








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




END_TEXT;

}

mysqli_free_result($get_post_res);
mysqli_free_result($topic_res);
mysqli_close($conn3);

$display_block .= "
回复 内容
回复人:$post_owner


创建时间:$post_create_time
$post_text


回复该帖
";
}

?>



查看帖子



查看帖子





replytopost.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']);
}

?>



回复帖子


回复[]的帖子。













}

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.1

SET 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 */;

回帖
  • 还没有人回复
本周热议
没有相关数据
layui

微信扫码关注 网盟开源 公众号