ThinkPHP基本的增刪查改操作實(shí)例教程
本文實(shí)例講述了ThinkPHP基本的增刪查改操作,是ThinkPHP項(xiàng)目開發(fā)中最常用的基礎(chǔ)操作,有著十分重要的應(yīng)用價(jià)值?,F(xiàn)將完整的實(shí)例代碼與大家分享,希望能對(duì)大家有所幫助。具體如下:
一、表aoli_user字段設(shè)置:
表aoli_user主要有以下幾個(gè)字段:
id username password createtime createip
二、view模板部分
1.用戶首頁模板:
aoli/Home/Tpl/default/User/index.html頁面代碼如下:
<form action="__URL__/add" method="post"> 用戶名:<input type="text" name="username" /><br /> 密碼:<input type="password" name="password" /><br /> 重復(fù)密碼:<input type="repassword" name="repassword" /><br /> <input type="submit" value="注冊(cè)" /> </form> <volist name="alist" id="vo"> <li><span>ID:</span>{$vo['id']}<span>用戶名:</span>{$vo['username']}<span>注冊(cè)ip:</span>{$vo['createip']}<a href="__URL__/del/id/{$vo['id']}">刪除</a> <a href="__URL__/edit/id/{$vo['id']}">編輯</a></li> </volist>
2.用戶編輯模板:
aoli/Home/Tpl/default/User/edit.html頁面代碼如下:
<form action="__URL__/update" method="post"> 用戶名:<input type="text" name="username" value="{$data['username']}" /><br /> 密碼:<input type="password" name="password" value="{$data['password']}" /><br /> IP:<input type="text" name="createip" value="{$data['createip']}" /><br /> 時(shí)間:<input type="text" name="createtime" value="{$data['createtime']}" /><br /> <input type="hidden" value="{$data['id']}" name="id" /> <input type="submit" value="更新" /> </form>
二、action部分:
aoli/Home/Lib/Action/UserAction.class.php頁面如下:
class UserAction extends Action { function index(){ $user=M('user'); $list=$user->field(array('id','username','createip'))->select(); $this->assign('title','thinkphp視頻演示'); $this->assign('alist',$list); $this->display(); } //刪除 function del(){ $user=D('user'); if($user->delete($_GET['id'])){ $this->success('刪除成功'); }else{ $this->error('刪除失敗'); } } //增加 function add(){ Load('extend'); if($_POST['password']!=$_POST['repassword']){ $this->error('兩次密碼不一致'); } $user=D('user'); if($vo=$user->create()){ $user->password=md5($user->password); $user->createtime=time(); //$user->createip=$_SERVER[]; $user->createip=get_client_ip(); if($user->add()){ $this->success('用戶注冊(cè)成功,返回上級(jí)頁面'); }else{ $this->error('用戶注冊(cè)失敗,返回上級(jí)頁面'); } }else{ $this->error($user->getError()); } } //顯示用戶的修改項(xiàng) function edit(){ $user=M('user'); $id=(int)$_GET['id']; $list=$user->where("id=$id")->find(); $this->assign('data',$list); $this->assign('title','顯示用戶編輯信息'); $this->display(); } //將更新數(shù)據(jù)寫入數(shù)據(jù)庫 function update(){ $user=M('user'); $user->password=md5($user->password); if($user->create()){ if($insertid=$user->save()){ $this->success('更新成功,受影響的行數(shù)為'.$insertid); }else{ $this->error('更新失敗'); } } } } ?>
感興趣的讀者可以在項(xiàng)目中調(diào)試運(yùn)行一下本文所述實(shí)例,以加深對(duì)ThinkPHP增刪查改操作的理解,方便在以后的項(xiàng)目中加以靈活運(yùn)用。
相關(guān)文章
PHP實(shí)現(xiàn)一個(gè)簡單url路由功能實(shí)例
這篇文章主要介紹了PHP實(shí)現(xiàn)一個(gè)簡單url路由功能實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2016-11-11Discuz批量替換帖子內(nèi)容的方法(使用SQL更新數(shù)據(jù)庫)
這篇文章主要介紹了Discuz批量替換帖子內(nèi)容的方法,使用SQL語句實(shí)現(xiàn),用REPLACE函數(shù)批量更新數(shù)據(jù)庫,需要的朋友可以參考下2014-06-06默默小談PHP&MYSQL分頁原理及實(shí)現(xiàn)
默默小談PHP&MYSQL分頁原理及實(shí)現(xiàn)...2007-01-01Yii2實(shí)現(xiàn)同時(shí)搜索多個(gè)字段的方法
這篇文章主要介紹了Yii2實(shí)現(xiàn)同時(shí)搜索多個(gè)字段的方法,結(jié)合實(shí)例形式分析了Yii2中同時(shí)搜索多個(gè)字段所使用的函數(shù)與具體使用方法,需要的朋友可以參考下2016-08-08laravel框架分組控制器和分組路由實(shí)現(xiàn)方法示例
這篇文章主要介紹了laravel框架分組控制器和分組路由實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了laravel框架分組控制器和分組路由的基本定義與使用方法,需要的朋友可以參考下2020-01-01ThinkPHP框架實(shí)現(xiàn)的郵箱激活功能示例
這篇文章主要介紹了ThinkPHP框架實(shí)現(xiàn)的郵箱激活功能,結(jié)合實(shí)例形式分析了thinkPHP使用class.smtp.php及class.phpmailer.php類文件進(jìn)行郵件發(fā)送實(shí)現(xiàn)激活功能的具體操作技巧,需要的朋友可以參考下2018-06-06