使用MySQL和PHP创建一个公告板

目录

一、创建表

二、制作首页(创建主题以及显示列表)

三、制作各个主题的页面(输入回帖和显示列表)

四、制作消息的查询界面

五、制作读取数据库信息的原始文件

六、制作数据重置页面

七、效果图

八、问题

1、目前无法处理此请求HTTP ERROR 500

2、The requested URL was not found on this server


一、创建表

tbj0(主题表)

tbj1(消息表)

表tbj0的列

列名 内容
group_c 用于输入主题的组号。列为INT类型且具有自动连续编号功能
topic_c

用于输入主题名,数据类型为VARCHAR(30)

date_c 用于输入创建主题的日期和时间。通过MySQL的NOW函数自动输入。数据类型为DATETIME
ip_c 用于存储发送信息的客户端的IP地址。不显示在浏览器上,而是作为出现特殊情况时的记录保留下来。这里暂且将数据类型设置为20个字符的字符串类型VARCHAR(20)

表tbj1的列

列名 内容
empid 用于存储所有主题中的回帖的编号
name 用于输入执行操作的人的姓名。数据类型为VARCHAR(30)
mess 用于输入消息。数据类型为TEXT
date_c 用于输入插入记录时的日期和时间,通过MySQL的NOW函数自动输入
group_c 用于存储主题的编号。作为和表tbj0连接时的键使用。数据类型为INT
ip_c 和表tbj0一样,用于存储发送信息的客户端的IP地址

二、制作首页(创建主题以及显示列表)

/**************bulletin_top.php*******************/

<?php

/******** 读取数据库信息等 **************/
require_once("data/db_info.php");

/********** 连接数据库,选择数据库 ***********/
$s=new pdo("mysql:host=$SERV;dbname=$DBNM",$USER,$PASS);

/************* 显示标题、图片等 **********/
print <<<eot1
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>"海贼王的页面</title>
</head>
<body style="background-color:silver">
<img src="pic/jk.gif" alt="小路飞">
<span style="color:purple;font-size:35pt">
海贼王的公告板!
</span>

<p>请点击要查看的主题编号</p>
<hr>
<div style="font-size:20pt">(主题列表)<div>
eot1;

/**************** 获取客户端IP地址 ******************/
$ip=getenv("REMOTE_ADDR");


/********** 如果主题名的变量$su_d有数据,则将其插入表tbj0***********/
$su_d=isset($_GET["su"])?htmlspecialchars($_GET["su"]):null;
if($su_d<>""){
  $s->query("INSERT INTO tbj0 (topic_c,date_c,ip_c) VALUES ('$su_d',now(),'$ip')");
}

$re=$s->query("SELECT * FROM tbj0");
while($result=$re->fetch()){
  print <<<eot2
  <a href="bulletin.php?gu=$result[0]">$result[0] $result[1]</a>
  <br>
  $result[2]创建<br><br>
  eot2;
}

/************** 用于创建主题的表单,以及查询页面的链接 *************/
print <<<eot3
  <hr>
  <div style="font-size:20pt">(创建主题)</div>
  请在这里创建新主题!
  <br>
  <form method="GET" action="bulletin_top.php">
  新创建主题的标题
  <input type="text" name="su" size="50">
  <div><input type="submit" value="创建"></div>
  </form>
  <hr>
  <span style="font-size:20pt">(查询消息)</span>
  <a href="bulletin_search.php">点击这里查询</a>
  <hr>
  </body>
  </html>
eot3;
?>

三、制作各个主题的页面(输入回帖和显示列表)

/**************bulletin.php*******************/

<?php

/*************** 读取数据库信息等 ***********/
require_once("data/db_info.php");

/**************** 连接数据库,选择数据库 *********/
$s=new PDO("mysql:host=$SERV;dbname=$DBNM",$USER,$PASS);

/*************** 获取主题的组号(gu),将其赋给$gu_d **********/
$gu_d=$_GET["gu"];

/*************** 如果$gu_d中包含数字以外的字符,则停止处理**********/
if(preg_match("/[^0-9]/",$gu_d)){
  print <<<eot1
  输入了非法的值<br>
  <a href="bulletin_top.php">请点击这里回到主题列表</a>
eot1;

/************** 如果$gu_d中不包含数字以外的字符,则按普通值处理 **********/
}elseif(preg_match("/[0-9]/",$gu_d)){

/************** 获取姓名和消息并删除标签 ****************/
$na_d=isset($_GET["na"])?htmlspecialchars($_GET["na"]):null;
$me_d=isset($_GET["me"])?htmlspecialchars($_GET["me"]):null;

/************** 获取IP地址 **************************/
$ip=getenv("REMOTE_ADDR");

/**************** 显示与主题组号(gu)相匹配的记录 **************/
$re=$s->query("SELECT topic_c FROM tbj0 WHERE group_c=$gu_d");
$result=$re->fetch();

/***************** 创建显示主题内容的字符串$topic_c_com **********/
$topic_c_com="「".$gu_d." ".$result[0]."」";

/**************** 输出主题显示的标题 *************/
print <<<eot2
  <!DOCTYPE html>
  <html>
  <head>
  <meta charset="UTF-8">
  <title>海贼王 $topic_c_com 主题 </title>
  </head>
  <body style="background-color:silver">
  <div style="color:purple;font-size:35pt">
  $topic_c_com 主题!
  </div>
  <br>
  <div style="font-size:18pt">$topic_c_com 的消息</div>
eot2;

/************ 如果输入了姓名($na_d),则将记录插入tbj1 **************/
if($na_d<>""){
  $re=$s->query("INSERT INTO tbj1 VALUES (0,'$na_d','$me_d',now(),$gu_d,'$ip')");
}

/*************** 显示出水平线 ***************/
print "<hr>";

/************** 按时间顺序显示回帖数据 *************/
$re=$s->query("SELECT * FROM tbj1 WHERE group_c=$gu_d ORDER BY date_c");

$i=1;
while($result=$re->fetch()){

  print "$i($result[0]):$result[1]:$result[3] <br>";
  print nl2br($result[2]);
  print "<br><br>";
      $i++;
}

print <<<eot3
  <hr>
  <div style="font-size:18pt">
  请在这里向 $topic_c_com 中写消息
  </div>
  <form method="GET" action="bulletin.php">
  <div>姓名 <input type="text" name="na"></div>
  消息
  <div>
  <textarea name="me" rows="10" cols="70"></textarea>
  </div>
  <input type="hidden" name="gu" value=$gu_d>
  <input type="submit" value="发送">
  </form>
  <hr>
  <a href="bulletin_top.php">返回主题列表</a>
  </body>
  </html>
eot3;

/************ 当$gu_d中不包含数字也不包含数字以外的字符时的处理 *************/
}else{
  print "请选择主题。<br>";
  print "<a href='bulletin_top.php'>点击这里返回主题列表</a>";
}
 ?>

四、制作消息的查询界面

/**************bulletin_search.php*******************/

<?php

/************ 读取数据库信息等 ****************/
require_once("data/db_info.php");


/************* 连接数据库,选择数据库 **************/
$s=new PDO("mysql:host=$SERV;dbname=$DBNM",$USER,$PASS);

/*************** 显示标题等 **************/
print <<<eot1
  <!DOCTYPE html>
  <html>
  <head>
  <meta charset="UTF-8">
  <title>海贼王的查询页面</title>
  </head>
  <body style="breakground-color:aqua">
  <hr>
  <div style="font-size:18pt">(查询结果如下)</div>
eot1;

/****************** 获取查询字符串并删除标签 ************/
$se_d=isset($_GET["se"])?htmlspecialchars($_GET["se"]):null;

/************* 如果查询字符串($se_d)中有数据,则执行查询处理 *********/
if($se_d<>""){

/************ 查询的SQL语句,连接表tbj1和表tbj0 ********************/
$str=<<<eot2
  SELECT tbj1.empid,tbj1.name,tbj1.mess,tbj0.topic_c
    FROM tbj1
  JOIN tbj0
  ON
      tbj1.group_c=tbj0.group_c
  WHERE tbj1.mess LIKE "%$se_d%"
eot2;

/************* 执行查询 ****************/
$re=$s->query($str);
while($result=$re->fetch()){
  print " $result[0] : $result[1] : $result[2] ($result[3])";
  print "<br><br>";
}
}

/************ 用于输入查询字符串的页面,以及指向首页的链接 **************/
print <<<eot3
  <hr>
  <div>请输入消息中含有的字符!</div>
  <form method="GET" action="bulletin_search.php">
  查询字符串
  <input type="text" name="se">
  <div>
  <input type="submit" value="查询">
  </div>
  </form>
  <br>
  <a href="bulletin_top.php">返回主题列表</a>
  </body>
  </html>
eot3;
 ?>

五、制作读取数据库信息的原始文件

/**************db_info.php*******************/

<?php
$SERV="localhost";
$USER="root";
$PASS="root";
$DBNM="db1";
 ?>

六、制作数据重置页面

/**************bulletin_reset.php*******************/

<?php
require_once("data/db_info.php");
$s=new PDO("mysql:host=$SERV;dbname=$DBNM",$USER,$PASS);

$s->query("DELETE FROM tbj0");
$s->query("DELETE FROM tbj1");
$s->query("ALTER TABLE tbj0 AUTO_INCREMENT=1");
$s->query("ALTER TABLE tbj1 AUTO_INCREMENT=1");

print "将海贼王的表初始化了";
 ?>

七、效果图

八、问题

1、目前无法处理此请求HTTP ERROR 500

找到对应版本的php.ini,将下面的值由off改为on,然后重启服务器。

display_errors = On
display_startup_errors = On

2、The requested URL was not found on this server

找到Apache文件下的httpd.conf配置文件,将下面的值由none改为all,然后重启服务器。

AllowOverride All

相关推荐

  1. MySQL教程-创建

    2024-04-01 09:52:05       26 阅读
  2. PHP MySQL 创建数据库

    2024-04-01 09:52:05       28 阅读
  3. Mysql创建博客相关的数据库

    2024-04-01 09:52:05       32 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-01 09:52:05       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-01 09:52:05       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-01 09:52:05       87 阅读
  4. Python语言-面向对象

    2024-04-01 09:52:05       96 阅读

热门阅读

  1. Ubuntu 16.04/18.04 LTS内核降级及通过GURB指定内核

    2024-04-01 09:52:05       40 阅读
  2. 算法思想 - 动态规划算法

    2024-04-01 09:52:05       40 阅读
  3. UniswapV2周边合约学习(五)-- ExampleFlashSwap.sol

    2024-04-01 09:52:05       44 阅读
  4. Mojo编程语言案例及介绍

    2024-04-01 09:52:05       45 阅读
  5. C++经典面试题目(十五)

    2024-04-01 09:52:05       39 阅读
  6. 微信小程序(3.3.5) 对实时帧数据调速

    2024-04-01 09:52:05       39 阅读
  7. 微信小程序——wxss和css的区别

    2024-04-01 09:52:05       42 阅读
  8. uView内置样式

    2024-04-01 09:52:05       44 阅读
  9. Github2024-03-31 开源项目日报 Top10

    2024-04-01 09:52:05       48 阅读