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

2023-10-27

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.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 */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

php--如何编写一个简易的论坛 的相关文章

  • 在 url 中传递百分号 (%) 并使用 php 获取其准确值

    我正在尝试在 url 中传递百分号 例如 B6011000995504101 SB 但当我回声时 它又回来了 011000995504101 SB 我想要与在 URL 中传递的值完全相同的值 我尝试使用 urlencode 函数 但它给了我
  • Codeigniter查看和回显

    我有一个在 codeigniter 中处理网页侧栏的函数 如下 function process sidebar this gt load gt view first access 1 this gt load gt view second
  • 获取结果到文本字段

    我有两个可以应用更改的表 但我需要回应基于特定标准所做的更改 现在 对于第一个表 所做的任何更改都会被回显 但是我不确定如果对第二个表 其他 进行更改 如何回显这些更改 if isset POST submit if isset POST
  • 如何在 PHP 中实现前向索引?

    我希望在 PHP 中实现一个简单的前向索引器 是的 我确实知道 PHP 并不是完成这项任务的最佳工具 但无论如何我还是想这样做 其背后的理由很简单 我想要一个 并且是 PHP 版本 让我们做一些基本假设 整个互联网包括 大约五千个 HTML
  • 无法将外键值插入链接表

    我目前正在尝试将数据插入名为的表中 客户报价 该表充当 顾客 表和 客户关税 桌子 它还记录通过以下方式提交数据的用户 user table 这是我的数据库的架构 https i stack imgur com gyCdb png http
  • MySQL 启动错误 - 根元素丢失

    我在 Windows Server 2003 R2 上安装 MySQL 大约两个月了 启动时 我们会看到一个错误 显示 高严重性错误 根元素丢失 然后是另一个高严重性错误 显示 在调用 WriteToLog 方法之前必须定义日志文件路径 任
  • 配置 htaccess 以使用 Angular 和 PHP 路由

    我正在尝试使用 Angular 4 和 PHP 路由 但我无法配置它以便同时使用两者 我可以让它与其中之一一起工作 但不能同时与两者一起工作 这是我的文件夹结构 root index html vendor bundle js other
  • 如何在 php 和 mongodb 中使用 findAndModify

    我想将 id 加 1 但运行 php 页面时出现问题 错误是 Fatal error Call to undefined method MongoCollection findAndModify in C wamp www 我的代码是
  • 我怎样才能完成笛卡尔积函数的 Objective-C 实现?

    作为我的问题的后续here https stackoverflow com questions 8176719 algorithm generating all combinations from items that must be ch
  • 为什么 Stripe Checkout 不将客户姓名添加到客户记录中?

    我正在尝试从 Stripe Checkout 表单提交中检索各种数据 我只是使用 Stripe 仪表板中提供的 Stripe Checkout 代码 In my checkout submission completed我有一个正在尝试检索
  • PHP 中的 JS charCodeAt 等效项(具有完整的 unicode 和 emoji 兼容性)

    我在 JS 中有一个简单的代码 如果涉及特殊字符 我无法在 PHP 中复制它 这是 JS 代码 参见JSFiddle https jsfiddle net h8oca3qg 5 用于输出 var str t char t and speci
  • PHP 中的基本 URL

    我有一个两难的困境 它已经困扰我很长一段时间了 我有一个本地测试服务器 其设置如下 127 0 0 1 我的网站在离线模式下如下所示 127 0 0 1 websitename index php 我的网站实时版本如下所示 websiten
  • 如何在 PHP 的 foreach 循环中获取两个项目? [复制]

    这个问题在这里已经有答案了 我有一个推荐轮播 轮播每次循环浏览两个项目 现在我想每次得到两个项目foreach环形 我怎么才能得到它 Code div div class row div class col md 6 div class s
  • PHP 中的并行处理 - 你是如何做到的?

    我目前正在尝试在 php 中实现一个作业队列 然后 队列将作为批处理作业进行处理 并且应该能够并行处理一些作业 我已经做了一些研究并找到了几种实现它的方法 但我并不太了解它们的优点和缺点 例如 通过多次调用脚本来进行并行处理fsockope
  • rewrite_mod 已启用,但 .htaccess 不起作用

    我在 Amazon EC2 的 ubuntu 12 04 中使用 apache 2 2 我使用启用了 mod rewrite sudo a2enmod rewrite 并能够看到 apache2ctl M 现在我编写了以下 htaccess
  • 使用 PHP 上传、调整图像大小并裁剪图像中心

    我想要创建一个非常非常基本的上传 调整大小和裁剪 PHP 脚本 其功能与 Twitter 用于上传头像图片的方法相同 无论如何我最后检查过 我希望脚本拍摄任何尺寸的图像 将最短边的大小调整为 116 像素 然后裁剪顶部和底部 如果是横向 则
  • Doctrine QueryBuilder 重用部件

    我想计算所有符合我的条件的字段 并使用学说查询生成器逐页获取它们 我生成的查询取决于我的过滤器字段 第一部分是计算记录 以便我可以计算页数 qb em gt createQueryBuilder qb gt select COUNT m i
  • 如何编辑 Woocommerce 单一产品元模板中显示的内容?

    我正在为客户做一些工作并使用 wordpress woocommerce 他们要求我将类别移动到我已经完成的单个产品页面上的产品名称下 但他们不希望它打印 类别 类别 1 类别 2 等 他们希望删除 类别 并且它实际上只列出类别的名称 而不
  • SQL 未插入到 Yii 中具有关系的表中

    我正在尝试创建一个用户 但所有值都没有插入到数据库中 Systems user 表与partys 表有关系 因为party id 是sytems user 的主键 没有插入任何内容 甚至没有错误 它只是返回到 创建 页面 这是我的架构 Ta
  • Laravel Eloquent with()-> 返回 null

    我正在尝试使用 Eloquent 来获取具有以下功能的特定产品 brand id映射到a的列brands表 该brand数组返回空 这里有什么明显需要改变的地方吗 product Product with images gt with br

随机推荐