首先是确定自己的留言板需求.例如:名字,邮件及留言内容.
一. 建立一个数据库guestbook。
CREATE TABLE IF NOT EXISTS `content` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(20) NOT NULL,
`email` varchar(50) NOT NULL,
`content` varchar(200) NOT NULL,
PRIMARY KEY (`id`))
ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=3;
二. 新建config.php
<?php
$q = mysql_connect("服务器","数据库用户","数据库密码");
if(!$q)
{
die('Could not connect: ' . mysql_error());
}
mysql_query("set names utf8"); //以utf8读取数据
mysql_select_db("guestbook",$q); //数据库
?>
三.新建index.php
<?php
include("config.php"); //引入数据库连接文件
$sql = "select * from content"; //搜索数据表content
$resule = mysql_query($sql,$q);
?>
<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<body>
<table width="678" align="center">
<tr>
<td colspan="2"><h1>留言本</h1></td>
</tr>
<tr>
<td width="586"><a href="index.php">首页</a> | <a href="liuyan.php">留言</a></td>
</tr>
</table>
<p>
<?
while($row=mysql_fetch_array($resule))
{
?>
</p>
<table width="678" border="1" align="center" cellpadding="1" cellspacing="1">
<tr>
<td width="178">Name:<? echo $row[1] ?></td>
<td width="223">Email:<? echo $row[2] ?></td>
</tr>
<tr>
<td colspan="4"><? echo $row[3] ?></td>
</tr>
<tr>
</table>
<?
}
?>
</body>
</html>
四.新建liuyan.php
<html>
<body>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<table width="678" align="center">
<tr>
<td colspan="2"><h1>留言本</h1></td>
</tr>
<tr>
<td width="586"><a href="index.php">首页</a> | <a href="liuyan.php">留言</a></td>
</tr>
</table>
<table align="center" width="678">
<tr>
<td>
<form name="form1" method="post" action="post.php">
<p>
Name:
<input name="name" type="text" id="name">
</p>
<p>Email:<input type="test" name="email" id="email"></p>
<p>
留言:
</p>
<p>
<textarea name="content" id="content" cols="45" rows="5"></textarea>
</p>
<p>
<input type="submit" name="button" id="button" value="提交">
<input type="reset" name="button2" id="button2" value="重置">
</p>
</form>
</td>
</tr>
</table>
</body>
</html>
五. 新建post.php
<?php
header("content-Type: text/html; charset=utf-8");
include("config.php");
$name= $_POST['name'];
$email= $_POST['email'];
$patch = $_POST['content'];
$content = str_replace("
","<br />",$patch);
$sql = "insert into content (name,email,content) values ('$name','$email','$content')";
mysql_query($sql);
echo "<script>alert('提交成功!返回首页。');location.href='index.php';</script>";
?>
这样已经成功的写出一个留言板了。