一:创建一个新的数据库 liuyanban
CREATE DATABASE liuyanban;
二:创建数据库表 message
CREATE TABLE `message` ( `id` tinyint(1) NOT NULL auto_increment, `user` varchar(25) NOT NULL, `title` varchar(50) NOT NULL, `content` tinytext NOT NULL, `lastdate` date NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
三:建立一个数据库连接文件
<?php /* * * @Authors peng--jun * @Email [email protected] * @Date 2015-05-17 10:53:44 * @Link http://www.cnblogs.com/xs-yqz/ * @version $Id$ ========================================== */ header("Content-type: text/html; charset=UTF-8"); $DBHOST = ‘localhost‘; $DBNAME = ‘liuyanban‘; $USER = ‘root‘; $PWD= ‘‘; $conn = mysql_connect($DBHOST,$USER,$PWD) or die ( "连接数据库失败" ); mysql_select_db ( $DBNAME, $conn ) or die ( "失败" ); ?>
四:页面代码
1 <?php 2 /* 3 * 4 * @Authors peng--jun 5 * @Email [email protected] 6 * @Date 2015-05-17 10:51:56 7 * @Link http://www.cnblogs.com/xs-yqz/ 8 * @version $Id$ 9 ===================================================== 10 */ 11 error_reporting(E_ALL ^ E_NOTICE); 12 include ‘include/connectfig.php‘;//引入头文件 13 if($_POST[‘submit‘]){ 14 $sql="insert into message(id,user,title,content,lastdate)values(‘‘,‘$_POST[user]‘,‘$_POST[title]‘,‘$_POST[content]‘,now())"; 15 mysql_query($sql); 16 } 17 ?> 18 19 <!doctype html> 20 <html> 21 <head> 22 <meta charset="UTF-8"> 23 <title>简易留言板</title> 24 <link rel="stylesheet" type="text/css" href="css/4_1.css"> 25 </head> 26 27 <body> 28 <div id="div1"> 29 30 <form method="post" id="form" action="4_1.php"> 31 用户:<input type="text" size="10" name="user"> 32 <br> 33 标题:<input type="text" name="title"> 34 <br> 35 内容: 36 <textarea name="content" cols="45" rows="5" name="content"></textarea> 37 <br> 38 <input type="submit" name="submit" value="留言" /> 39 </form> 40 41 42 <table cellpadding="5" cellspacing="1" > 43 <?php 44 $sql1 = "select * from message order by id desc";//按照id倒叙排列 45 $query = mysql_query($sql1); 46 while ($rows = mysql_fetch_array($query)) { 47 ?> 48 <tr bgcolor="#eff3ff"> 49 <td><b>用户:</b><?php echo $rows[‘title‘];?> <b>标题:</b><?php echo $rows[‘user‘] ;?> <b>发布时间:</b><?=$rows[‘lastdate‘];?> 50 </td> 51 </tr> 52 <tr bgColor="#ffffff"> 53 <td><b>内容:</b><?php echo $rows[‘content‘];?></td> 54 </tr> 55 <?php 56 } 57 ?> 58 </table> 59 </div> 60 </body> 61 </html>
时间: 2024-11-05 11:49:46