bbs by php

原创
2011/09/16 23:01
阅读数 177

include.php:

<?php
function doDB()
{
 global $conn;
 $conn = mysql_connect("localhost","root","admin") or die("Connect to mysql fail,something was wrong!");
 mysql_select_db("adamli");
 mysql_query("set names utf8");
}
?>

add_topic.php:

<?php header("content-type:text/html;charset=utf-8"); ?>
<h3>发帖</h3>
<form action="do_addtopic.php" method="post">
<p>
<strong>您的邮箱:</strong><br><input type="text" name="topic_owner" size="40" maxlength="150" />
</p>
<p>
<strong>帖子主题:</strong><br><input type="text" name="topic_title" size="40" maxlength="150" />
</p>
<p><strong>贴子内容:</strong><br>
<textarea name="post_text" rows="6" cols="60"></textarea>
</p>
<p><input type="submit" name="submit" value="发贴"></p>
</form>
do_addtopic.php:

<?php
header("content-type:text/html;charset=utf-8");
include_once("include.php");
doDB();
if(trim($_POST[topic_owner])==""||trim($_POST[topic_title])==""||trim($_POST[post_text])=="")
{
header("location: add_topic.php");
exit;
}
$add_topic = "insert into topics values('','$_POST[topic_title]','$_POST[topic_owner]',now())";
mysql_query($add_topic) or die("Query into topics fail,something was wrong!");
$topic_id = mysql_insert_id();
$add_post = "insert into posts values('','$topic_id','$_POST[post_text]','$_POST[topic_owner]',now())";
mysql_query($add_post) or die("Query into posts fail,something was wrong!");
mysql_close($conn);
echo "<script>alert('发贴成功!');window.location.href='topiclist.php';</script>";
?>
topiclist.php:

<?php
header("content-type:text/html;charset=utf-8");
include_once("include.php");
doDB();
$topic_sql = "select * from topics order by topic_create_time desc";
$topic_res = mysql_query($topic_sql) or die("Query from topics fail,something was wrong!");
if(mysql_num_rows($topic_res)<1)
{
 header("location: add_topic.php");
 exit;
}
$display = "<h1>帖子列表:</h1><table width='800' border='1'><tr><td><div align='center'><strong>主题</strong></div></td><td><div align='center'><strong>共含几条内容</strong></div></td></tr>";
while($topic_res_info = mysql_fetch_object($topic_res))
{
$topic_id = $topic_res_info->topic_id;
$topic_title = $topic_res_info->topic_title;
$topic_owner = $topic_res_info->topic_owner;
$topic_create_time = $topic_res_info->topic_create_time;
$post_sql = "select count(post_id) as post_num from posts where topic_id='".$topic_id."'";
$post_res = mysql_query($post_sql) or die("query from post fail!something was wrong!");
while($post_res_info = mysql_fetch_object($post_res))
{
$post_num = $post_res_info->post_num;
}
$display .= "<tr><td><strong><a href='show_post.php?id=$topic_id'>".$topic_title."</a><strong><br>于<font color=red>".$topic_create_time."</font> 由 <font color=red>".$topic_owner."</font>发表。</td><td align='center'>".$post_num."</td></tr>";
}
mysql_free_result($topic_res);
mysql_free_result($post_res);
mysql_close($conn);
$display.="</table><p><a href='add_topic.php'>发贴</a></p>";
echo $display;
?>
show_post.php:

<?php
header("content-type:text/html;charset=utf-8");
include_once("include.php");
doDB();
if(!$_GET['id'])
{
header("topiclist.php");
exit;
}
$topic_sql = "select topic_title from topics where topic_id='".$_GET['id']."'";
$topic_res = mysql_query($topic_sql) or die("query from topics fail,something was wrong!");
while($topic_res_info = mysql_fetch_object($topic_res))
{
$topic_title = $topic_res_info->topic_title;
}
$post_sql = "select * from posts where topic_id='".$_GET['id']."'";
$post_res = mysql_query($post_sql) or die("query from posts fail,something was wrong!");
$display = "<h1>显示帖子</h1><p><font color=red>".$topic_title."</font>里的内容:</p><table width='800' border='1'><tr><td><div align='center'><strong>发表者</strong></div></td><td><div align='center'><strong>发表内容</strong></div></td></tr>";
while($post_res_info = mysql_fetch_object($post_res))
{
$post_id = $post_res_info->post_id;
$post_owner = $post_res_info->post_owner;
$post_create_time = $post_res_info->post_create_time;
$post_text = $post_res_info->post_text;
$display .= "<tr><td>".$post_owner."<br />[".$post_create_time."]</td><td>".$post_text."<br /><div align='right'><a href='reply_post.php?id=$post_id'>回复</a></div></td></tr>";
}
$display .= "</table><p><a href='add_topic.php'>发贴</a>&nbsp;&nbsp;<a href='topiclist.php'>贴子列表</a></p>";
echo $display;
?>
reply_post.php:

<?php
header("content-type:text/html;charset=utf-8");
include_once("include.php");
doDB();
if(!$_POST)
{
if(!$_GET['id'])
{
header("topiclist.php");
exit;
}
}
/*
$topic_sql = "select topic_id from posts where post_id='".$_GET['id']."'";
$topic_res = mysql_query($topic_sql);
while($topic_res_info = mysql_fetch_object($topic_res))
{
$topic_id = $topic_res_info->topic_id;
}

$topic = "select topic_title from topics where topic_id='".$topic_id."'";
$query = mysql_query($topic);
while($res = mysql_fetch_object($query))
{
$topic_title = $res->topic_title;
}
*/
$topic_sql = "select topics.topic_id,topics.topic_title from posts
left join topics on posts.topic_id=topics.topic_id where posts.post_id='".$_GET['id']."'";
$topic_res = mysql_query($topic_sql);
while($topic_res_info = mysql_fetch_object($topic_res))
{
$topic_id = $topic_res_info->topic_id;
$topic_title = $topic_res_info->topic_title;
}
if($_POST)
{
if(trim($_POST[topic_owner])==""||trim($_POST[post_text])=="")
{
echo "<script>alert('请输入你的邮箱和回复内容');window.location.href='reply_post.php?id=$_POST[topic_id]';</script>";
exit;
}
$post_sql = "insert into posts values('','$_POST[topic_id]','$_POST[post_text]','$_POST[topic_owner]',now())";
mysql_query($post_sql) or die("Reply to post fail,something was wrong!");
echo "<script>alert('回复内容成功!');window.location.href='show_post.php?id=$_POST[topic_id]'</script>";
}
mysql_free_result($topic_res);
mysql_close($conn);
?>
<h1>提交你的回复到贴<font color=red><?php echo $topic_title; ?></font>中:</h1>
<form action="reply_post.php" method="post">
<p><strong>您的邮箱:</strong><br>
<input type="text" name="topic_owner" size="40" maxlength="150"></p>
<p><strong>您要回复的内容:</strong><br>
<textarea name="post_text" rows="6" cols="60"></textarea></p>
<p><input type="hidden" name="topic_id" value="<?=$topic_id?>"></p>
<p><input type="submit" name="submit" value="回复"></p>
</form>

此bbs会用到两张表,结构如下:

mysql> desc posts;desc topics;
+------------------+--------------+------+-----+---------+----------------+
| Field            | Type         | Null | Key | Default | Extra          |
+------------------+--------------+------+-----+---------+----------------+
| post_id          | int(11)      | NO   | PRI | NULL    | auto_increment |
| topic_id         | int(11)      | NO   |     |         |                |
| post_text        | text         | YES  |     | NULL    |                |
| post_owner       | varchar(150) | YES  |     | NULL    |                |
| post_create_time | datetime     | YES  |     | NULL    |                |
+------------------+--------------+------+-----+---------+----------------+
5 rows in set (0.08 sec)

+-------------------+--------------+------+-----+---------+----------------+
| Field             | Type         | Null | Key | Default | Extra          |
+-------------------+--------------+------+-----+---------+----------------+
| topic_id          | int(11)      | NO   | PRI | NULL    | auto_increment |
| topic_title       | varchar(150) | YES  |     | NULL    |                |
| topic_owner       | varchar(150) | YES  |     | NULL    |                |
| topic_create_time | datetime     | YES  |     | NULL    |                |
+-------------------+--------------+------+-----+---------+----------------+
4 rows in set (0.03 sec)

展开阅读全文
打赏
0
2 收藏
分享
加载中
更多评论
打赏
0 评论
2 收藏
0
分享
返回顶部
顶部