话说也看了不少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,用来存储留言.
- CREATE TABLE `content` (
- `id` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
- `username` VARCHAR( 16 ) NOT NULL ,
- `content` VARCHAR( 255 ) NOT NULL ,
- `insert_time` DATETIME NOT NULL
- )
ID:流水号
username:留言者名称
content:留言内容
insert_time:留言添加的时间
2.共有三个文件,congif.php,add.php和index.php。
新建congif.php文件,用来写数据库配置和连接,在文件里引用这个页面,就可以连接数据库。
- <?php
- /*配置数据库*/
- $DB_HOST='localhost';//数据库地址
- $DB_USER='root'; //数据库用户名
- $DB_PWD=''; //数据库密码,我这里是空
- $DB_NAME='ibook'; //数据库名称
- /*下面是链接数据库的代码,不用修改*/
- $link=mysql_connect($DB_HOST,$DB_USER,$DB_PWD)or die('数据库连接错误:'.mysql_error());
- mysql_select_db($DB_NAME);
- mysql_query("SET NAMES 'utf8'");//设置编码
- ?>
3.新建add.php文件,用来添加留言。
- <?php
- require('congif.php');
- if($_GET['action']=='save'){
- //接收数据
- $username=trim($_POST['username']);
- $content=trim($_POST['content']);
- $time=date('Y-m-d H:i:s');
- if (!get_magic_quotes_gpc()) {//如果魔术引号关闭使用addslashes转换
- $username=addslashes($username);
- $content=addslashes($content);
- }
- //判断表单是否全部填写
- if (!$username || !$content) {
- echo '请输入用户名的内容!';
- exit;
- }
- if (strlen($username)>16) {
- echo '用户名超出长度!';
- exit;
- }
- if (strlen($content)>255) {
- echo '内容超出长度!';
- exit;
- }
- $sql="insert into content(username,content,insert_time) values('$username','$content','$time')";
- mysql_query($sql,$link);
- echo '添加成功! <a href="index.php">查看留言</a>';
- exit;
- }
- ?>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
- <html>
- <head>
- <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
- <title>提交留言</title>
- <link rel="stylesheet" href="/css/master.css" type="text/css" media="screen" title="no title" charset="utf-8" />
- </head>
- <body>
- <form id="from1" name="form1" action="add.php?action=save" method="post" accept-charset="utf-8">
- <div>
- 用户名:<br/>
- <input type="text" name="username" value="" id="username"><br/>
- 内容:<br/>
- <textarea name="content" rows="8" cols="40"></textarea>
- <p><input type="submit" value="Continue →"></p>
- </div>
- </form>
- </body>
- </html>
4.新建index.php页面,作用是显示留言列表。
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
- <html>
- <head>
- <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
- <title>iNotebook</title>
- <link rel="stylesheet" href="/css/master.css" type="text/css" media="screen" title="no title" charset="utf-8" />
- </head>
- <body>
- <h1>Welcome to iNoteBook!!</h1>
- <div><a href="add.php">添加留言</a></div>
- <div>
- <?php
- require('congif.php');
- //查询,并显示
- $result=mysql_query("select * from content order by id desc");
- while ($row=mysql_fetch_array($result,MYSQL_BOTH)) {//取一条数据
- ?>
- <div id="content">
- <div id="content_header">
- [<?php echo htmlspecialchars($row['username']) ?>]发表于:<?php echo htmlspecialchars($row['insert_time']) ?>
- </div>
- <div>
- <?php echo htmlspecialchars($row['content']) ?>
- </div><br />
- </div>
- <?php
- }
- mysql_free_result($result);
- ?>
- </div>
- </form>
- </body>
- </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 内容相关的,灌水、攻击性或不恰当的评论 可能 会被编辑或删除。

跟我差不多一样懒啊,哇嘎嘎。
[Reply]