《PHP教程:ThinkPHP基本的增刪查改操作實(shí)例教程》要點(diǎn):
本文介紹了PHP教程:ThinkPHP基本的增刪查改操作實(shí)例教程,希望對(duì)您有用。如果有疑問,可以聯(lián)系我們。
PHP編程本文實(shí)例講述了ThinkPHP基本的增刪查改操作,是ThinkPHP項(xiàng)目開發(fā)中最常用的基礎(chǔ)操作,有著十分重要的應(yīng)用價(jià)值.現(xiàn)將完整的實(shí)例代碼與大家分享,希望能對(duì)大家有所幫助.具體如下:
PHP編程一、表aoli_user字段設(shè)置:
PHP編程表aoli_user主要有以下幾個(gè)字段:
PHP編程id?? username?? password??? createtime??? createip
PHP編程二、view模板部分
PHP編程1.用戶首頁(yè)模板:
PHP編程aoli/Home/Tpl/default/User/index.html頁(yè)面代碼如下:
PHP編程
<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>
PHP編程2.用戶編輯模板:
PHP編程aoli/Home/Tpl/default/User/edit.html頁(yè)面代碼如下:
PHP編程
<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>
PHP編程二、action部分:
PHP編程aoli/Home/Lib/Action/UserAction.class.php頁(yè)面如下:
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í)頁(yè)面');
}else{
$this->error('用戶注冊(cè)失敗,返回上級(jí)頁(yè)面');
}
}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ù)庫(kù)
function update(){
$user=M('user');
$user->password=md5($user->password);
if($user->create()){
if($insertid=$user->save()){
$this->success('更新成功,受影響的行數(shù)為'.$insertid);
}else{
$this->error('更新失敗');
}
}
}
}
?>
PHP編程感興趣的讀者可以在項(xiàng)目中調(diào)試運(yùn)行一下本文所述實(shí)例,以加深對(duì)ThinkPHP增刪查改操作的理解,方便在以后的項(xiàng)目中加以靈活運(yùn)用.
維易PHP培訓(xùn)學(xué)院每天發(fā)布《PHP教程:ThinkPHP基本的增刪查改操作實(shí)例教程》等實(shí)戰(zhàn)技能,PHP、MYSQL、LINUX、APP、JS,CSS全面培養(yǎng)人才。
轉(zhuǎn)載請(qǐng)注明本頁(yè)網(wǎng)址:
http://www.snjht.com/jiaocheng/14813.html