简单留言板的实现

 

 

 

1、创建一个工程 bbs

 

2、 创建一个文件用来连接数据库 conn.php

<?php
$conn = @ mysql_connect("localhost", "root", "") or die("数据库链接错误");
mysql_select_db("bbs", $conn);
mysql_query("set names 'GBK'"); //使用GBK中文编码;
?>

3、创建一个文件来新增 留言add.php

<?php
include("conn.php");

if($_POST['submit']){
 $sql = "INSERT INTO message (id,user,title,content,lastdate) "
 ."values('','$_POST[user]','$_POST[title]','$_POST[content]',now())";

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

  <form action="add.php" method="post">
 用户:<input type="text" size="10" name="user" /><br/>
 标题:<input type="text" name="title" /><br/>
  内容:<textarea name="content"></textarea><br/>

   <input type="submit" name="submit" value="发布留言"/>

  </form>

 

 

4、创建一个文件,用来查看列表 list.php

<?php

 include("conn.php");

?>
<table width=500 border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#add3ef">
 <?
 $sql = "SELECT * FROM message order by id desc";
 $query = mysql_query($sql);
 while($row=mysql_fetch_array($query)){
 ?>
  <tr bgcolor="#eff3ff">
  <td>标题:<?=$row[title]?> 用户:<?=$row[user]?> 发布时间:<?=$row[lastdate]?></td>
  </tr>
  <tr bgColor="#ffffff">
  <td>内容:<?=$row[content]?> </td>
  </tr>
  <?
   }
  ?>
</table>