首先数据库的SQL如下:

-- -- 表的结构 `message` --

CREATE TABLE `message` (   `id` int(10) NOT NULL auto_increment,   `user` varchar(25) character set utf8 NOT NULL,

`title` varchar(50) character set utf8 NOT NULL,   `content` tinytext character set utf8 NOT NULL,   `lastdate` date NOT NULL,

PRIMARY KEY  (`id`) ) ENGINE=InnoDB  DEFAULT CHARSET=gbk AUTO_INCREMENT=11 ;

程序代码如下:

首先创建config.php,代码如下:

<?php $conn = @mysql_connect("localhost","root","") or die("数据库连接出错!");

mysql_select_db("lyb",$conn); mysql_query("set names utf8"); ?>

然后创建index.php,代码如下:

<form action="index.php" method="post"> <table border="1" align="center" > 用户:<input type="text" name="user" /><br>

标题:<input type="text" name="title" /><br /> 内容:<textarea name="content"></textarea><br />

<input type="submit" name="submit" value="提交" /></table> </form>

<?php include("config.php");if($_POST['submit'] && !empty($_POST['title'])&& !empty($_POST['user'])&& !empty($_POST['content']))

{ $sql="insert into message (id,user,title,content,lastdate) values ('','$_POST[user]','$_POST[title]','$_POST[content]',now())";

mysql_query($sql); //echo "成功"; }?>

<table width=500 border="1" align="left" cellpadding="5" cellspacing="1" bgcolor="#add3ef">

<?php $sql="select * from message order by id desc"; $query=mysql_query($sql); while($row=mysql_fetch_array($query)){ ?>

<tr bgcolor="#eff3ff"> <td>标题:<?php echo $row[title];?> 用户:<?php echo $row[user];?></td> </tr> <tr bgColor="#ffffff">

<td>内容:<?php echo $row[content];?></td> </tr>

<?php } ?>

转载请注明转自:运达's blog(原文地址:http://www.yunda51.com/?p=408)