php--如何编写一个简易的论坛
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 */;
php--如何编写一个简易的论坛相关推荐
- python写一个文件下载器_Python3使用TCP编写一个简易的文件下载器
原标题:Python3使用TCP编写一个简易的文件下载器 利用Python3来实现TCP协议,和UDP类似.UDP应用于及时通信,而TCP协议用来传送文件.命令等操作,因为这些数据不允许丢失,否则会造 ...
- python 正则的使用 —— 编写一个简易的计算器
python 正则的使用 -- 编写一个简易的计算器 在 Alex 的博客上看到的对正则这一章节作业是编写一个计算器,要求能计算出下面的算式. 1 - 2 * ( (60-30 +(-40/5) * ...
- Android 中编写一个简易购物车,商品包括商品名称,单价,数量,可以对商品进行增删改查功能。(ArrayList,SQLite)
Android 中编写一个简易购物车,商品包括商品名称,单价,数量,可以对商品进行增删改查功能.(ArrayList,SQLite) 布局(activity_main.xml): <?xml v ...
- 仿照Windows的计算器,编写一个简易的计算器程序,实现加、减、乘、除等运算。
仿照Windows的计算器,编写一个简易的计算器程序,实现加.减.乘.除等运算. 偷懒了很多,将就着用吧: import java.awt.FlowLayout; import java.awt.ev ...
- 十位数连加 c语言,用C语言编写一个简易计算器可实现加减乘除,连加连减,连乖连除....
用C语言编写一个简易计算器可实现加减乘除,连加连减,连乖连除. 用C语言编写一个简易计算器可实现加减乘除,连加连减,连乖连除. 人气:435 ℃时间:2020-04-10 06:55:13 优质解答 ...
- 如何编写一个简易网络爬虫
感谢小臣投稿 本文将简述网络爬虫及其工作流程,结合个人实践,简单介绍如何使用HttpClient.HtmlParser第三方jar工具包,编写一个简易的网络爬虫. 网络爬虫简述及流程架构 网络爬虫,又 ...
- python9行代码_如何用9行Python代码编写一个简易神经网络
原标题:如何用9行Python代码编写一个简易神经网络 Python部落(python.freelycode.com)组织翻译,禁止转载,欢迎转发. 学习人工智能时,我给自己定了一个目标--用Pyth ...
- 利用java swing编写一个简易的计算器,实现了括号,优先级,三角函数,阶乘等功能
利用java swing编写一个简易的计算器 背景 效果图 一.默认图 二.计算三角函数 三.阶乘运算 四.常见的四则运算(实现了优先级) 代码 本文借鉴了"初识Java,实现简易计算器(带 ...
- 使用python编写一个简易的打折程序
使用python编写一个简易的打折程序
最新文章
- Linux主机被黑过程和简单处理
- 初识广播机制(监听网络状态的改变,监听网络是否可以使用)
- 14. Leetcode 80. 删除有序数组中的重复项 II (数组-同向双指针-快慢指针)
- android菜单点击功能怎么做的,单击android中的Menu按钮打开上下文菜单
- why there is always popup window for HANA
- node --- [express] cookie/session 机制与 中间件的使用(路由守卫)
- 搭建于 Cubieboard 之上的超小型实时监控平台 - mjpg篇
- python关于文件的编程题_《Python编程》源代码文件
- 2017 到 2018,PWA 技术到底经历了什么
- Redmi K30S更多细节曝光:骁龙865加持 提供多款配色
- [LeetCode]Trapping Rain Water
- 杭电多校HDU 6601 Keen On Everything But Triangle(主席树)题解
- Opencv 移植 (亲测有效)
- 如何制作一个优秀的PPT?
- 三次样条插值的缺点_三次样条插值
- sequence_item、sequence、sequencer、driver的关系
- 最新机器人视觉系统介绍,给机器人装上“眼睛”
- Android+SpringBoot+Vue实现安装包前台上传,后台管理,移动端检测自动更新
- Lombok介绍、使用方法和总结
- 内容推荐场景中自监督学习的应用