13
Sep

菜鸟写留言薄-1

话说也看了不少PHP的资料了,现在尝试开始做个留言薄。我是跟着这个教程做的,根据自己的情况,做了一些小小的修改。高手请多多指教。

我的开发环境是,WinXP+XAMPP套件+e-texteditor
XAMPP下载地址:http://www.apachefriends.org/zh_cn/xampp-windows.html
e-texteditor下载地址:http://www.e-texteditor.com/

1.创建数据库,名称iBook,然后建立表content,用来存储留言.

  1. CREATE TABLE `content` (
  2. `id` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
  3. `username` VARCHAR( 16 ) NOT NULL ,
  4. `content` VARCHAR( 255 ) NOT NULL ,
  5. `insert_time` DATETIME NOT NULL
  6. )

ID:流水号
username:留言者名称
content:留言内容
insert_time:留言添加的时间

2.共有三个文件,congif.php,add.php和index.php。
新建congif.php文件,用来写数据库配置和连接,在文件里引用这个页面,就可以连接数据库。

  1. <?php
  2.     /*配置数据库*/
  3.     $DB_HOST='localhost';//数据库地址
  4.     $DB_USER='root';      //数据库用户名
  5.     $DB_PWD='';          //数据库密码,我这里是空
  6.     $DB_NAME='ibook';      //数据库名称
  7.  
  8.     /*下面是链接数据库的代码,不用修改*/
  9.     $link=mysql_connect($DB_HOST,$DB_USER,$DB_PWD)or die('数据库连接错误:'.mysql_error());
  10.     mysql_select_db($DB_NAME);
  11.     mysql_query("SET NAMES 'utf8'");//设置编码
  12. ?>

3.新建add.php文件,用来添加留言。

  1. <?php
  2. require('congif.php');
  3.     if($_GET['action']=='save'){
  4.  
  5.         //接收数据
  6.         $username=trim($_POST['username']);
  7.         $content=trim($_POST['content']);
  8.  
  9.         $time=date('Y-m-d H:i:s');
  10.  
  11.         if (!get_magic_quotes_gpc()) {//如果魔术引号关闭使用addslashes转换
  12.             $username=addslashes($username);
  13.             $content=addslashes($content);
  14.         }
  15.         //判断表单是否全部填写
  16.         if (!$username || !$content) {
  17.             echo '请输入用户名的内容!';
  18.             exit;
  19.         }
  20.         if (strlen($username)>16) {
  21.             echo '用户名超出长度!';
  22.             exit;
  23.         }
  24.         if (strlen($content)>255) {
  25.             echo '内容超出长度!';
  26.             exit;
  27.         }
  28.  
  29.         $sql="insert into content(username,content,insert_time) values('$username','$content','$time')";
  30.  
  31.         mysql_query($sql,$link);
  32.         echo '添加成功! <a href="index.php">查看留言</a>';
  33.         exit;
  34.     }
  35.  
  36. ?>
  37. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  38.     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  39. <html>
  40.     <head>
  41.         <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
  42.         <title>提交留言</title>
  43.         <link rel="stylesheet" href="/css/master.css" type="text/css" media="screen" title="no title" charset="utf-8" />
  44.     </head>
  45.     <body>
  46.         <form id="from1" name="form1" action="add.php?action=save" method="post" accept-charset="utf-8">
  47.             <div>
  48.                 用户名:<br/>
  49.                 <input type="text" name="username" value="" id="username"><br/>
  50.                 内容:<br/>
  51.                 <textarea name="content" rows="8" cols="40"></textarea>
  52.                 <p><input type="submit" value="Continue →"></p>
  53.         </div>
  54.         </form>
  55.     </body>
  56. </html>

4.新建index.php页面,作用是显示留言列表。

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  2.     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  3. <html>
  4.     <head>
  5.         <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
  6.         <title>iNotebook</title>
  7.         <link rel="stylesheet" href="/css/master.css" type="text/css" media="screen" title="no title" charset="utf-8" />
  8.     </head>
  9.     <body>
  10.  
  11.         <h1>Welcome to iNoteBook!!</h1>
  12.         <div><a href="add.php">添加留言</a></div>
  13.         <div>
  14.         <?php
  15.             require('congif.php');
  16.             //查询,并显示
  17.             $result=mysql_query("select * from content order by id desc");
  18.             while ($row=mysql_fetch_array($result,MYSQL_BOTH)) {//取一条数据
  19.         ?>
  20.         <div id="content">
  21.             <div id="content_header">
  22.                 [<?php echo htmlspecialchars($row['username']) ?>]发表于:<?php echo htmlspecialchars($row['insert_time']) ?>
  23.             </div>
  24.             <div>
  25.                 <?php echo htmlspecialchars($row['content']) ?>
  26.             </div><br />
  27.         </div>
  28.         <?php
  29.         }
  30.         mysql_free_result($result);
  31.  
  32.         ?>
  33.         </div>
  34.         </form>
  35.     </body>
  36. </html>

这个超级简单的留言板就完成了,仅仅有添加和显示的功能。删除和修改的页面和添加差不多,只不过sql语句不一样。

1 条评论了已经

发表评论

名字(必须)
邮箱(不会被公布)(必须)
网址

字体为 粗体 是必填项目,邮箱地址 永远不会 公布。

允许部分 HTML 代码:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
URLs must be fully qualified (eg: http://ipeng.org),and all tags must be properly closed.

超出部分系统将会自动分段及换行。

请保证评论内容是与日志或 Blog 内容相关的,灌水、攻击性或不恰当的评论 可能 会被编辑或删除。

    订阅

      不建议订阅此Blog!点击下图者,后果自负!

    搜索

    标签云

    页面

    分类

    最新日志

    贵圈真乱

    5天前更新:让文字说话吧(MAC下的语音朗读) Macis | 他用MBP30天没更新,都长草了 无忧纪 | 山寨货寨主6天前更新:逝去的2008与迷茫的2009 胡戈戈 | 沙发之王昨日更新:请问如何去掉图片超连接四周的边框! 萧过无痕 | 大雪也无痕

    关于

    搜索

    标签云