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字段占用,因此在文件命名時(shí)不得使用形如“l(fā)ist.html”的命名方式)
預(yù)期頁面:
下面就利用MVC架構(gòu)設(shè)計(jì)模式對(duì)其進(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ī)號(hào)碼:</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">添加時(shí)間:</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模式部分,個(gè)人目前對(duì)這一部分的理解是 用來嚴(yán)重添加數(shù)據(jù)的合法性和給出錯(cuò)誤提示 。實(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í)間不能為空") ); }
最后是純粹的邏輯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("添加失敗!"); } }
以上就是整個(gè)實(shí)現(xiàn)過程了,希望對(duì)大家的學(xué)習(xí)有所幫助
友情鏈接thinkPHP參考手冊(cè): http://document.thinkphp.cn/manual_3_2.html
原文作者:橙色時(shí)光
- 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)證注冊(cè)功能
- ThinkPHP3.2.2實(shí)現(xiàn)持久登錄(記住我)功能的方法
- thinkPHP實(shí)現(xiàn)MemCache分布式緩存功能
- thinkphp實(shí)現(xiàn)圖片上傳功能
- thinkPHP實(shí)現(xiàn)簽到功能的方法
相關(guān)文章
PHP自帶函數(shù)給數(shù)字或字符串自動(dòng)補(bǔ)齊位數(shù)
很多時(shí)候我們需要對(duì)數(shù)字進(jìn)行格式化,比如位數(shù)不足前面加0補(bǔ)足。用 php可以很輕易實(shí)現(xiàn),因?yàn)镻HP自帶了相關(guān)功能的函數(shù)。2014-07-07phpmailer發(fā)送gmail郵件實(shí)例詳解
本篇文章是對(duì)phpmailer發(fā)送gmail郵件實(shí)例進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06PHP安全之register_globals的on和off的區(qū)別
這篇文章主要介紹了PHP安全之register_globals的on和off的區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07PHP實(shí)現(xiàn)的簡(jiǎn)單組詞算法示例
這篇文章主要介紹了PHP實(shí)現(xiàn)的簡(jiǎn)單組詞算法,涉及php針對(duì)字符串的遍歷、遞歸、組合、運(yùn)算等相關(guān)操作技巧,需要的朋友可以參考下2018-04-04超級(jí)好用的一個(gè)php上傳圖片類(隨機(jī)名,縮略圖,加水印)
可生成隨機(jī)的名稱,縮略圖,加水印.2010-06-06php和js實(shí)現(xiàn)根據(jù)子網(wǎng)掩碼和ip計(jì)算子網(wǎng)功能示例
這篇文章主要介紹了php和js實(shí)現(xiàn)根據(jù)子網(wǎng)掩碼和ip計(jì)算子網(wǎng)功能,結(jié)合實(shí)例形式分析了PHP與js針對(duì)IP地址子網(wǎng)掩碼計(jì)算的相關(guān)操作技巧,需要的朋友可以參考下2019-11-11