使用PHP,MySql来制作简单的留言板。
HTML表单标签:
<form action="" method="GET" > <!-- action: 提交的URL method: 提交类型,POST/GET -->
text1: <input type="text" size=10 name="user"/>
<br>
text2: <input type="text" size=20 name="title"/>
<br>
<input type="submit" name="submit" value="提交" />
<textarea name="content"></textarea> <!-- //多文本框的名称 -->
</form>
PHP页面调用:
include(); 放在 PHP 程序的任何一个位置,PHP 程序在执行到时,才会先读入 include 所指定引入的文件,如果出现错误将会提示. 如: include("con.php");
include_once() 同一文件只读取一次
require(); 放在 PHP 程序的最前面,PHP 程序在执行前,就会先读入 require 所指定引入的文件.
require_once() 同一文件只读取一次
留言板文件构成:
数据库文件 mycon.php
初始界面文件 catalog.php
操作文件 add.php
列表文件 list.php
因为:
CREATE TABLE `message` (
`id` tinyint(1) NOT NULL auto_increment,
`user` varchar(25) NOT NULL,
`title` varchar(50) NOT NULL,
`content` text NOT NULL,
`lastdate` date NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk AUTO_INCREMENT=1 ;
mysql_query($sql); 出现错误:
The mysql extension is deprecated and will be removed in the future
所以还是用mysqli_query系列的语句。。。
mycon.php
<?php
$link=mysqli_connect("localhost:3306","root","****","db_study");
if(!$link){
die("cound not connect");
}
mysqli_query($link,"set names 'UTF8'");
function htmtocode($content) {
// str_replace: "\n" --> "<br>" 第三个参数是内容
$content = str_replace(" ", " ", $content);
$content = str_replace("\n", "<br>", $content);
$content = str_replace("'", "`", $content);
return $content;
}
?>
catalog.php:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>welcome to massage board</title>
<hr>
<a href="add.php">
添加留言
</a> |
<a href="list.php">
浏览留言
</a>
</b>
<hr size=2>
<table width=200 border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#add3ef">
<b>
<tr bgcolor="#eff3ff">
<td>
welcome to message board!
</td>
</tr>
</table>
list.php
<?php
include("mycon.php");
?>
<table width=700 border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#add3ef">
<?php
$sql="select * from message";
$result=mysqli_query($link,$sql);
while($row=mysqli_fetch_array($result)){
?>
<tr bgcolor="#eff3ff">
<td>
标题:<?php echo $row['title']?>
<br>
用户:<?php echo $row['user']?>
</td>
</tr>
<tr bgColor="#ffffff">
<td><span style="font-family:微软雅黑;font-size:14px;color:#7f7f7f;font-style: normal;
font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 21px;
orphans: auto; text-align: left; text-indent: 0px; text-transform: none; white-space:
normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color:
rgb(255, 255, 255); display: inline !important; float: none;">
内容:<br><?php echo htmtocode($row['content']) ?></span>
</td>
</tr>
<?php
}
?>
</table>
add.php
<?php
include("mycon.php");
if(!empty($_POST['submit'])){
//echo
$sql=$sql = "INSERT INTO `message`(`id`, `user`, `title`, `content`, `lastdate`)
VALUES ('','$_POST[user]','$_POST[title]','$_POST[content]',now())";
mysqli_query($link,$sql);
}
?>
<form input="myadd.php" method="POST">
用户: <input type="text" size=10 name="user"/> <br>
标题: <input type="text" size=28 name="title"><br>
内容:
<textarea name="content" cols="80" rows="10" ></textarea><br>
<input type="submit" name="submit" value="发表留言">
效果:
catalog:
add:
list: