ThinkPHP学习笔记(八)一个用户增删改查的小例子

主要是action文件的方法实现:

conf文件

  1. <?php  
  2. $selfConfig = array(  
  3.   
  4. //更换模式最好删除一些~app.php和~runtime.php  
  5.   
  6. //'配置项'=>'配置值'  
  7. //因为开启URL重新不论是被重写的还是没被重写的,都可以通过原有路径访问  
  8. //如果想开启rewrite模式,需要做如下操作  
  9. //1.query服务器已经开启了Apache的rewrite模块  
  10. //  LoadModule rewrite_module modules/mod_rewrite.so  
  11. //2.在与主入口文件,统计目录下,新建一个.htaccess(vi:save .htaccess;记事本:".htaccess")  
  12. //如果选用模式2(rewrite)会加大服务器的消耗  
  13. 'URL_MODEL'=>1,  
  14.   
  15. 'URL_PATNINFO_MODEL'=>2,  
  16. //pathinfo包含两类  
  17.     //1普通模式:加上m和a:顺序关系可以发生变化  
  18.     //http://localhost/MyThinkPHP/admin.php/m/index/a/index  
  19.     //传值  
  20.     //http://localhost/MyThinkPHP/admin.php/m/index/a/index/username/zhangsan/password/password  
  21.     //2智能识别模块操作(默认模式就是智能识别)  
  22.     //http://localhost/MyThinkPHP/admin.php/index/index  
  23.     //传值  
  24.     //http://localhost/MyThinkPHP/admin.php/index/index/username/zhangsan/password/password  
  25.           
  26.   
  27. //修改URL分隔符  
  28. //'URL_PATHINFO_DEPR'=>'-',  
  29.   
  30. //修改模板左右定界符  
  31. 'TMPL_L_DELIM'=>'<!--{',  
  32. 'TMPL_R_DELIM'=>'}-->',  
  33.   
  34.   
  35. //********************************非常华丽的分割线**************************************  
  36.   
  37. //开启调试模式  
  38. //1.模拟linux系统来识别大小写  
  39. //2.方法名的大小写与模板文件大小写有关  
  40. //注意:在分帧页面中,不能有body,但是app_dubug的信息是属于body体中的内容  
  41. 'APP_DEBUG'=>true,  
  42. //可以自定义页面的Trace信息  
  43. //配置文件路径的Trace信息配置在Thinkphp/Tpl下的pageTrace.tpl.php  
  44. //自定义方式:  
  45. //'TMPL_TRACE_FILE'=>APP_PATH.'/Public/trace.php',  
  46. //或者自定义个trace.php页面放入当前的Conf文件夹中  
  47.   
  48.   
  49. //默认调试文件的位置:  
  50. //ThinkPHP/Common/debug.php  
  51. //不缓存数据库字段;如果开启,再修改可以将Runtim/Data下面的文件进行删除  
  52. //'DB_FIELDS_CACHE'=> false,  
  53. //可以自定义的debug.php放入当前的Conf文件夹中  
  54.   
  55.   
  56. //先将APP_DEBUG设置为false然后在加入下面参数  
  57. //'APP_DEBUG'=>false,  
  58. //显示运行次此页面需要的时间  
  59. //'SHOW_RUN_TIME'=>true,  
  60. //显示详细的运行时间(基于SHOW_RUN_TIME)  
  61. //'SHOW_ADV_TIME'=>true,  
  62. //显示数据库的操作次数(基于SHOW_RUN_TIME)  
  63. //'SHOW_DB_TIMES'=>true,  
  64. //显示缓存的操作次数(基于SHOW_RUN_TIME)  
  65. //'SHOW_CACHE_TIMES'=>true,  
  66. //显示内存的开销(基于SHOW_RUN_TIME)  
  67. //'SHOW_USE_MEM'=>true,  
  68.   
  69.   
  70.   
  71. //设置模板  
  72. //'DEFAULT_THEME'=>'default',  
  73.   
  74.   
  75.   
  76. //日志处理log类:lib/Think/Core/log.class.php  
  77. //开启日志  
  78. //'LOG_RECORD'=>true,  
  79. //日志处理log类:lib/Think/Core/log.class.php中有处理级别,可以选择性的加入  
  80. //'LOG_RECORD_LEVEL'=>array('EMERG','ALERT'),  
  81.   
  82.   
  83. //由于数据库的链接需要多个项目来使用可以在一个页面中定义个公共的配置项,返回一个array数组  
  84. //连接数据库设置  
  85. //'DB_TYPE'=>'mysql',  
  86. //'DB_HOST'=>'localhost',  
  87. //'DB_NAME'=>'hibernate',  
  88. //'DB_USER'=>'root',  
  89. //'DB_PWD'=>'root',  
  90. 如果未修改可以不用填写  
  91. //'DB_POST'=>'3306',  
  92. //'DB_PREFIX'=>'tb_',  
  93.   
  94.   
  95. //令牌相关操作  
  96. //'TOKEN_ON'=>true,  
  97. //'TOKEN_NAME'=>'__hash__',  
  98. //'TOKEN_TYPE'=>'md5',  
  99.   
  100.   
  101. );  
  102.   
  103. $databaseConfig = include './database.php';  
  104.   
  105. //连接返回之后并不好用,只能直接返回自定义的配置信息,可能是我的配置有问题,先留下这个问题  
  106. return array_merge($selfConfig,$databaseConfig);  
  107. //return $selfConfig;  
  108. ?>  

外部引入的数据库链接文件和配置
  1. <?php  
  2. return array(  
  3.   
  4. //链接数据库的方式:见DatabaseAction.class.php  
  5.   
  6.   
  7. //主从数据库的配置(Common/convention.php)  
  8. //1.开启数据库的分布式  
  9. //    'DB_DEPLOY_TYPE'=> 1, // 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器)  
  10. //2.必须要做数据库服务器中进行相应的配置  
  11. //百度设置数据库集群  
  12. //3.读写分离(默认是第一台服务器是写入服务器,其他的服务器的读服务器)  
  13. //    'DB_RW_SEPARATE'=> true,// 数据库读写是否分离 主从式有效  
  14. //ThinkPHP默认的字符集是utf8,不要加中划线-   
  15. //  'DB_FIELDTYPE_CHECK'=> false, // 是否进行字段类型检查  
  16. //    'DB_FIELDS_CACHE'   => true,  // 启用字段缓存  
  17. //    'DB_CHARSET'        => 'utf8',// 数据库编码默认采用utf8  
  18.       
  19.   
  20.   
  21.   
  22. //由于数据库的链接需要多个项目来使用可以在一个页面中定义个公共的配置项,返回一个array数组  
  23. //ThinkPHP中的db目录:Lib/Think/Db/Db.class.php  
  24. //连接数据库设置  
  25. 'DB_TYPE'=>'mysql',  
  26. 'DB_HOST'=>'localhost',  
  27. //设置主从数据时用  
  28. //'DB_HOST'=>'localhost,192.168.123.1',  
  29. 'DB_NAME'=>'thinkphp',  
  30. //设置主从数据时若名字不同  
  31. //'DB_NAME'=>'hibernate,ant,thinkphp',  
  32. 'DB_USER'=>'root',  
  33. 'DB_PWD'=>'root',  
  34. //如果未修改可以不用填写  
  35. 'DB_POST'=>'3306',  
  36. 'DB_PREFIX'=>'tb_',  
  37. );  
  38. ?>  

action
  1. <?php  
  2. class UserdbAction extends Action{  
  3.     public function index(){  
  4.         $user=M('User');  
  5.         $list=$user->select();  
  6.         $this->assign('title','thinkphp演示');  
  7.         $this->assign('alist',$list);  
  8.           
  9.         $this->display();  
  10.     }  
  11.     public function add(){  
  12.         //D是需要些Model的,M不需要写  
  13.         $user=D('User');  
  14.         if ($vo=$user->create()){  
  15.             echo 'create成功';  
  16.             $user->password=md5($user->password);  
  17.             $user->createtime=time();  
  18.             //扩展函数需要进加载之后使用  
  19.             load('extend');  
  20.             $user->createip=get_client_ip();  
  21.             if ($user->add()){  
  22.                 $this->success("用户注册成功");  
  23.             }else{  
  24.                 $this->error($user->getError());  
  25.             }  
  26.         }else{  
  27.             echo 'create失败';  
  28.             $this->error($user->getError());  
  29.         }  
  30.     }  
  31.     public function del(){  
  32.         //D是需要些Model的,M不需要写  
  33.         $user=D('User');  
  34.         if ($vo=$user->delete($_GET['id'])){  
  35.             $this->success("用户删除成功");  
  36.         }else{  
  37.             $this->error($user->getError());  
  38.         }  
  39.     }  
  40.     public function edit(){  
  41.         $user=M('user');  
  42.         $id=$_GET['id'];  
  43.         $list=$user->where("id=$id")->find();  
  44.           
  45.         $this->assign('user',$list);  
  46.         $this->assign('title','编辑用户');  
  47.         $this->display();  
  48.     }  
  49.     public function update(){  
  50.         $user=M('user');  
  51.         if ($vo=$user->create()){  
  52.             if ($lineNum=$user->save()){  
  53.                 $this->success("用户更新成功");  
  54.             }else{  
  55.                 $this->error($user->getError());  
  56.             }  
  57.         }else{  
  58.             $this->error($user->getError());  
  59.         }  
  60.     }  
  61. }  
  62. ?>  

model
  1. <?php  
  2.     class UserModel extends Model{  
  3.         function modelTest(){  
  4.             echo '测试的跨模型操作,调用模型中的方法';  
  5.         }  
  6.     }  
  7. ?>  

html:

index.html

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  2. <html>  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  5. <title><!--{$title}--></title>  
  6. </head>  
  7. <body>  
  8. <form action="__URL__/add" method="post">  
  9.     用户名:<input type="text" name="username">  
  10.     密码:<input type="text" name="password">  
  11.     <input type="submit" value="注册">  
  12.     <!--{__NOTOKEN__}-->  
  13. </form>  
  14.   
  15.   
  16. <voList name="alist" id="vo">  
  17.     <li><span>ID</span><!--{$vo['id']}-->  
  18.     <span>用户名</span><!--{$vo['username']}-->  
  19.     <span>IP</span><!--{$vo['createip']}-->  
  20.     <a href="__URL__/del/id/<!--{$vo['id']}-->">删除</a>  
  21.     <a href="__URL__/edit/id/<!--{$vo['id']}-->">编辑</a></li>  
  22. </voList>  
  23. </body>  
  24. </html>  

edit.html
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  2. <html>  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  5. <title><!--{$title}--></title>  
  6. </head>  
  7. <body>  
  8. <form action="__URL__/update" method="post">  
  9.     用户名:<input type="text" name="username" value="<!--{$user['username']}-->"><br/>  
  10.     密码:<input type="text" name="password" value="<!--{$user['password']}-->"><br/>  
  11.     ip:<input type="text" name="createip" value="<!--{$user['createip']}-->"><br/>  
  12.     创建时间:<input type="text" name="createtime" value="<!--{$user['createtime']}-->"><br/>  
  13.     <input type="submit" value="更新">  
  14.     <!--{__NOTOKEN__}-->  
  15.     <input type="hidden" value="<!--{$user['id']}-->" name="id">  
  16. </form>  
  17. </body>  
  18. </html> 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值