网站地图    收藏   

主页 > 后端 > thinkphp教程 >

ThinkPHP基本的增删查改操作 - Thinkphp

来源:自学PHP网    时间:2014-11-28 23:17 作者: 阅读:

[导读] 表aoli_user字段:id username password createtime createipaoli Home Tpl default User index html:formaction=__URL__ add...

ThinkPHP基本的增删查改操作

表aoli_user字段:

id   username   password    createtime    createip

aoli/Home/Tpl/default/User/index.html:

  1. <form action="__URL__/add" method="post"> 
  2.   用户名:<input type="text" name="username" /><br /> 
  3.   密码:<input type="password" name="password" /><br /> 
  4.   重复密码:<input type="repassword" name="repassword" /><br /> 
  5.   <input type="submit" value="注册" /> 
  6. </form> 
  7. <volist name="alist" id="vo"> 
  8.   <li><span>ID:</span>{$vo['id']}<span>用户名:</span>{$vo['username']}<span>注册ip:</span>{$vo['createip']}<a href="__URL__/del/id/{$vo['id']}">删除</a>&nbsp;&nbsp;<a href="__URL__/edit/id/{$vo['id']}">编辑</a></li> 
  9. </volist> 

aoli/Home/Tpl/default/User/edit.html:

  1. <form action="__URL__/update" method="post"> 
  2.   用户名:<input type="text" name="username" value="{$data['username']}" /><br /> 
  3.   密码:<input type="password" name="password" value="{$data['password']}" /><br /> 
  4.   IP:<input type="text" name="createip" value="{$data['createip']}" /><br /> 
  5.   时间:<input type="text" name="createtime" value="{$data['createtime']}" /><br /> 
  6.   <input type="hidden" value="{$data['id']}" name="id" /> 
  7.   <input type="submit" value="更新" /> 
  8. </form> 

aoli/Home/Lib/Action/UserAction.class.php:

  1. class UserAction extends Action { 
  2.      function index(){ 
  3.          $user=M('user'); 
  4.          $list=$user->field(array('id','username','createip'))->select(); 
  5.          $this->assign('title','thinkphp视频演示'); 
  6.          $this->assign('alist',$list); 
  7.          $this->display();          
  8.      } 
  9.      //删除 
  10.      function del(){ 
  11.          $user=D('user'); 
  12.          if($user->delete($_GET['id'])){ 
  13.              $this->success('删除成功');      
  14.          }else
  15.              $this->error('删除失败'); 
  16.          } 
  17.      } 
  18.      //增加 
  19.      function add(){ 
  20.          Load('extend'); 
  21.          if($_POST['password']!=$_POST['repassword']){ 
  22.              $this->error('两次密码不一致');      
  23.          } 
  24.          $user=D('user'); 
  25.          if($vo=$user->create()){ 
  26.              $user->password=md5($user->password); 
  27.              $user->createtime=time(); 
  28.              //$user->createip=$_SERVER[]; 
  29.              $user->createip=get_client_ip(); 
  30.              if($user->add()){ 
  31.                  $this->success('用户注册成功,返回上级页面');      
  32.              }else
  33.                  $this->error('用户注册失败,返回上级页面'); 
  34.              } 
  35.          }else
  36.              $this->error($user->getError());      
  37.          } 
  38.      } 
  39.      //显示用户的修改项 
  40.      function edit(){ 
  41.          $user=M('user'); 
  42.          $id=(int)$_GET['id']; 
  43.          $list=$user->where("id=$id")->find(); 
  44.          $this->assign('data',$list); 
  45.          $this->assign('title','显示用户编辑信息');  
  46.          $this->display(); 
  47.      } 
  48.      //将更新数据写入数据库 
  49.      function update(){ 
  50.          $user=M('user'); 
  51.          $user->password=md5($user->password); 
  52.          if($user->create()){ 
  53.              if($insertid=$user->save()){ 
  54.                  $this->success('更新成功,受影响的行数为'.$insertid); 
  55.              }else
  56.                  $this->error('更新失败');      
  57.              } 
  58.                        
  59.          } 
  60.      } 

自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习

京ICP备14009008号-1@版权所有www.zixuephp.com

网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com

添加评论