登錄界面:
?
數據庫和數據表的結構
?
具體的操作步驟如下:
第一步:入口文件index.php內容 (此文件基本是屬于固定的格式)
<?php
define('THINK_PATH','./ThinkPHP/');
define('APP_NAME','MyApp');
define('APP_PAHT','./MyApp/');
require_once THINK_PATH.'ThinkPHP.php';
$app=new App();
$app->run();
?>
?
第二步:Active文件夾中的IndexAction.class.php文件內容
<?php
class IndexAction extends Action
{
?public function Index()
?{
??$this->display();//渲染到模板index.html
?}
?
?// 生成驗證碼??
?public function verify()//這是一個固定的格式
?{??
??import("ORG.Util.Image");??
??Image::buildImageVerify();??
?} ?
?
?//檢驗驗證碼是否正確??
?public function verifyCheck()
?{ ????
??if (md5($_POST['verifyTest']) != Session::get('verify'))
??{??
???die('驗證碼錯誤');??//如果驗證碼不對就退出程序
??}?
?}???
?
??function insert()
??{
??header('Content-Type:text/html; charset=utf-8');//防止出現亂碼
??$this->verifyCheck();//調用本類的函數,
??$Pagemodel = D("user");
??$vo = $Pagemodel->create();
??if(false === $vo) die($Pagemodel->getError());
??$topicid = $Pagemodel->add(); //add方法會返回新添加的記錄的主鍵值
??if($topicid) echo "數據庫添加成功";
??else throw_exception("數據庫添加失敗");
?}
}
?>
?
第三步:寫模板文件,也就是寫LIB文件夾中的HTML文件
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文檔</title>
??<style type="text/css">
????#form1
?{
??width:250px;
??height:250px;
??margin:20px auto;
??border:1px #039 solid;
??padding:20px 20px;
?}
??</style>
??<script type='text/javascript'>
?function freshVerify()
?{??
???document.getElementById('verifyImg').src='__URL__/verify/'+Math.random();??
?}??
??</script>??
</head>
<body>
??<form name="form1" id="form1" method="post" action="__URL__/insert">
????注冊帳號:<br /><br />
????帳號:<input type="text" name="user" id="user" maxlength="16" /><br /><br />
????密碼:<input type="password" name="password" id="password" maxlength="16" /><br /><br />
????Q Q:<input type="text" name="qq" id="qq" maxlength="16" /><br /><br />
???
????驗證碼:<input type='text' name='verifyTest' size="5">?
????<img style='cursor:pointer' title='刷新驗證碼' src='__URL__/verify' id='verifyImg' onClick='freshVerify()'/> <br /><br />
??????
????<input type="submit" name="btn1" id="btn1" value="提交" />
????<input type="reset" name="btn2" id="btn2" value="重置" />
??</form>
</body>
</html>
注意:
1、也就是一個form,action="__URL__/insert"表示提交到當前Action類(即IndexAction.class.php文件中)的insert函數;
2、此模板(靜態網頁)中的各個name要與user數據表的各個字段是一樣的名字,否則在insert函數中數據不能自動進庫。
3、驗證碼的刷新由靜態網頁負責。值相等由IndexAction類的verifyCheck()負責。
?
第四步:寫Model類,在model目錄中,文件名為:UserModel.class.php
<?php
??class UserModel extends Model//User與數據庫中數據表的名字相同
??{
???var $_validate=array??//自動驗證
???(
???array('user','require','賬號不能為空',1),??//1表示必須驗證
???array('qq','number','QQ號必須是數字,注冊失敗!',2),//2表示不為空時要驗證
???array('user','','此帳號己經存在!',0,'unique','add')??//不能有同時賬號出現
???);???
???var $_auto=array
???(
?????????array('password','md5','add','function'),??//密碼用md5加密后填入數據表中
?????????array('create_time','time','add','function')??//在增加時自動將時間擢填入表中
???);
??}
?>
注解:
1、文件名,類名必須用user,因為數據庫中對應的是user表;
2、其實只要寫一個框架就行了:
??class UserModel extends Model
??{
??}
但為什么還要var $_validate=array()和var $_auto=array()呢?那是因為:
var $_validate=array()是自動驗證的意思,var $_auto=array()是自動填充的意思。
自動驗證就是驗證數據的格式是否正確,自動填充就是你不輸入的值,它自動給你灌進去,比如'create_time'建立時間,我們在模板中沒有這個,但這里它就自動進庫了。