thinkphp框架實(shí)現(xiàn)數(shù)據(jù)添加和顯示功能
最近的幾篇隨筆將都從thinkPHP框架的使用上著筆,好了,廢話不多說,下面是干貨。
這篇文章將圍繞采用thinkPHP框架 向數(shù)據(jù)庫中添加數(shù)據(jù) 和 在網(wǎng)頁中顯示 這兩項(xiàng)功能進(jìn)行展示。
目的:在add頁添加數(shù)據(jù)后在lists頁進(jìn)行顯示(注意:由于thinkPHP框架已經(jīng)將list字段占用,因此在文件命名時不得使用形如“l(fā)ist.html”的命名方式)
預(yù)期頁面:

下面就利用MVC架構(gòu)設(shè)計(jì)模式對其進(jìn)行實(shí)現(xiàn)
首先利用表單提交方式實(shí)現(xiàn)V視圖部分,代碼如下:
<form role="form" method="post" action="__MODULE__/Admin/User/doAdd">
<div class="input-group"> <span class="input-group-addon">用<img src="__PUBLIC__/end/images/em.png" alt="" width="6" height="20">戶<img src="__PUBLIC__/end/images/em.png" alt="" width="6" height="20">名:</span>
<input type="text" class="form-control" placeholder="" name="username">
</div>
<div class="input-group "> <span class="input-group-addon" for="inputWarning1">真實(shí)姓名:</span>
<input type="text" class="form-control" placeholder="" id="input" name="realname">
</div>
<div class="input-group"> <span class="input-group-addon">手機(jī)號碼:</span>
<input type="text" class="form-control" placeholder="" name="telphone">
</div>
<div class="input-group"> <span class="input-group-addon">電子郵箱:</span>
<input type="text" class="form-control" placeholder="" name="email">
</div>
<div class="input-group"> <span class="input-group-addon">添加時間:</span>
<input type="text" class="form-control" placeholder="2014-05-22" name="resgistertime">
</div>
<div class="input-group"> <span class="input-group-addon">設(shè)置密碼:</span>
<input type="text" class="form-control" placeholder="123456" name="password">
</div>
<div class="input-group"> <span class="input-group-addon">確認(rèn)密碼:</span>
<input type="text" class="form-control" placeholder="123456" name="repassword">
</div>
<div class="input-group">
<button type="submit" class="btn btn-primary "> 保<img src="__PUBLIC__/end/images/em.png" alt="" width="20" height="20">存 </button>
</div>
</form>
接下來是M模式部分,個人目前對這一部分的理解是 用來嚴(yán)重添加數(shù)據(jù)的合法性和給出錯誤提示 。實(shí)現(xiàn)代碼如下:
<?php
namespace Admin\Model;
use Think\Model;
class AdminUsersModel extends Model {
public $_validate = array (
array("username", "require", "用戶名不能為空"),
array("realname", "require", "真實(shí)姓名不能為空"),
array("password", "require", "密碼不能為空"),
array("repassword", "require", "確認(rèn)密碼不能為空"),
array("telphone", "require", "電話不能為空"),
array("email", "require", "郵箱不能為空"),
array("resgistertime", "require", "注冊時間不能為空")
);
}
最后是純粹的邏輯C控制器部分啦,實(shí)現(xiàn)代碼如下:
public function add(){
$this->display();
}
public function doAdd(){
if (!IS_POST) {
exit("bad request!");
}
$adminUsersModel = D("AdminUsers");
if (!$adminUsersModel->create()) {
$this->error($adminUsersModel->getError());
}
if ($adminUsersModel->add()) {
$this->success("添加成功!",U("Admin/User/lists"));
}
else{
$this->error("添加失敗!");
}
}
以上就是整個實(shí)現(xiàn)過程了,希望對大家的學(xué)習(xí)有所幫助
友情鏈接thinkPHP參考手冊: http://document.thinkphp.cn/manual_3_2.html
原文作者:橙色時光
- php+mysql+jquery實(shí)現(xiàn)日歷簽到功能
- ThinkPHP+jquery實(shí)現(xiàn)“加載更多”功能代碼
- Thinkphp整合微信支付功能
- thinkphp實(shí)現(xiàn)分頁顯示功能
- thinkPHP統(tǒng)計(jì)排行與分頁顯示功能示例
- thinkPHP交易詳情查詢功能詳解
- thinkPHP商城公告功能開發(fā)問題分析
- thinkPHP訂單數(shù)字提醒功能的實(shí)現(xiàn)方法
- Thinkphp實(shí)現(xiàn)短信驗(yàn)證注冊功能
- ThinkPHP3.2.2實(shí)現(xiàn)持久登錄(記住我)功能的方法
- thinkPHP實(shí)現(xiàn)MemCache分布式緩存功能
- thinkphp實(shí)現(xiàn)圖片上傳功能
- thinkPHP實(shí)現(xiàn)簽到功能的方法
相關(guān)文章
如何在symfony中導(dǎo)出為CSV文件中的數(shù)據(jù)
如果您需要在symfony中將數(shù)據(jù)庫中的數(shù)據(jù)導(dǎo)出為CSV文件,試試這個2011-10-10
php生成并下載word文件到本地實(shí)現(xiàn)方法詳解
要給最常用出租屋管理系統(tǒng)增加個合同功能,mark下知識點(diǎn)。要生成合同就需要使用phpword。文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
php數(shù)據(jù)流中第K大元素的計(jì)算方法及代碼分析
在本篇文章里小編給大家整理了一篇關(guān)于php數(shù)據(jù)流中第K大元素的計(jì)算方法及代碼分析內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2021-07-07
PHP學(xué)習(xí)筆記之三 數(shù)據(jù)庫基本操作
本文介紹最基本最實(shí)用的數(shù)據(jù)庫操作。首先簡單復(fù)習(xí)下MySQL的使用方法,并且建好一張表備用。MySQL在Linux系統(tǒng)上一般都是裝好的,在win下安裝也很簡單,不多做介紹。2011-01-01
php數(shù)組函數(shù)序列之a(chǎn)rray_unique() - 去除數(shù)組中重復(fù)的元素值
array_unique() 函數(shù)移除數(shù)組中的重復(fù)的值,并返回結(jié)果數(shù)組。 當(dāng)幾個數(shù)組元素的值相等時,只保留第一個元素,其他的元素被刪除。2011-10-10

