第一步:配置好测试环境;(详细略了)
第二部:新建一个数据库,命名为guestbook(名字可以随便改),可以直接在phpmyadmin里面操作,在数据库里面新建一张表‘content’,表里面有4个字段:id,name,email,content。也可以写sql语句运行。
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`)
)
第三步:新建一个config.php,链接数据库的
<?php
$q=mysql_connect("localhost","root","");
if(!$q){
die('亲,链接不到数据库哦!'.mysql_error());
}
mysql_query("set name utf8"); //以utf8读取数据库
mysql_select_db("guestbook",$q); //数据库
?>
第四步:新建一个liuyan.php,填写留言信息的
<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>
第五步:新建一个post.php,用post方法提交
<?php
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>";
?>
最后新建一个首页文件index.php输出添加成功的留言信息。
<?php
include("config.php"); //链接数据库
$sql="select * from content";
$resule=mysql_query($sql, $q);
?>
<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>
<?php
while($row=mysql_fetch_array($resule)){
?>
</p>
<table width="678" border="1" align="center" cellpadding="1" cellspacing="1">
<tr>
<td width="178">Name:<?php echo $row[1] ?></td>
<td width="223">Email:<?php echo $row[2] ?></td>
</tr>
<tr>
<td colspan="4"><?php echo $row[3] ?></td>
</tr>
<tr>
</table>
<?php } ?>
最后的最后可以了,感受一下自己的杰作吧
http://jingyan.baidu.com/article/bea41d435b7695b4c51be6f8.html