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

来源:互联网 发布:手机淘宝怎么买彩票 编辑:程序博客网 时间:2024/05/16 18:26

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

conf文件

<?php$selfConfig = array(//更换模式最好删除一些~app.php和~runtime.php//'配置项'=>'配置值'//因为开启URL重新不论是被重写的还是没被重写的,都可以通过原有路径访问//如果想开启rewrite模式,需要做如下操作//1.query服务器已经开启了Apache的rewrite模块//LoadModule rewrite_module modules/mod_rewrite.so//2.在与主入口文件,统计目录下,新建一个.htaccess(vi:save .htaccess;记事本:".htaccess")//如果选用模式2(rewrite)会加大服务器的消耗'URL_MODEL'=>1,'URL_PATNINFO_MODEL'=>2,//pathinfo包含两类//1普通模式:加上m和a:顺序关系可以发生变化//http://localhost/MyThinkPHP/admin.php/m/index/a/index//传值//http://localhost/MyThinkPHP/admin.php/m/index/a/index/username/zhangsan/password/password//2智能识别模块操作(默认模式就是智能识别)//http://localhost/MyThinkPHP/admin.php/index/index//传值//http://localhost/MyThinkPHP/admin.php/index/index/username/zhangsan/password/password//修改URL分隔符//'URL_PATHINFO_DEPR'=>'-',//修改模板左右定界符'TMPL_L_DELIM'=>'<!--{','TMPL_R_DELIM'=>'}-->',//********************************非常华丽的分割线**************************************//开启调试模式//1.模拟linux系统来识别大小写//2.方法名的大小写与模板文件大小写有关//注意:在分帧页面中,不能有body,但是app_dubug的信息是属于body体中的内容'APP_DEBUG'=>true,//可以自定义页面的Trace信息//配置文件路径的Trace信息配置在Thinkphp/Tpl下的pageTrace.tpl.php//自定义方式://'TMPL_TRACE_FILE'=>APP_PATH.'/Public/trace.php',//或者自定义个trace.php页面放入当前的Conf文件夹中//默认调试文件的位置://ThinkPHP/Common/debug.php//不缓存数据库字段;如果开启,再修改可以将Runtim/Data下面的文件进行删除//'DB_FIELDS_CACHE'=> false,//可以自定义的debug.php放入当前的Conf文件夹中//先将APP_DEBUG设置为false然后在加入下面参数//'APP_DEBUG'=>false,//显示运行次此页面需要的时间//'SHOW_RUN_TIME'=>true,//显示详细的运行时间(基于SHOW_RUN_TIME)//'SHOW_ADV_TIME'=>true,//显示数据库的操作次数(基于SHOW_RUN_TIME)//'SHOW_DB_TIMES'=>true,//显示缓存的操作次数(基于SHOW_RUN_TIME)//'SHOW_CACHE_TIMES'=>true,//显示内存的开销(基于SHOW_RUN_TIME)//'SHOW_USE_MEM'=>true,//设置模板//'DEFAULT_THEME'=>'default',//日志处理log类:lib/Think/Core/log.class.php//开启日志//'LOG_RECORD'=>true,//日志处理log类:lib/Think/Core/log.class.php中有处理级别,可以选择性的加入//'LOG_RECORD_LEVEL'=>array('EMERG','ALERT'),//由于数据库的链接需要多个项目来使用可以在一个页面中定义个公共的配置项,返回一个array数组//连接数据库设置//'DB_TYPE'=>'mysql',//'DB_HOST'=>'localhost',//'DB_NAME'=>'hibernate',//'DB_USER'=>'root',//'DB_PWD'=>'root',////如果未修改可以不用填写//'DB_POST'=>'3306',//'DB_PREFIX'=>'tb_',//令牌相关操作//'TOKEN_ON'=>true,//'TOKEN_NAME'=>'__hash__',//'TOKEN_TYPE'=>'md5',);$databaseConfig = include './database.php';//连接返回之后并不好用,只能直接返回自定义的配置信息,可能是我的配置有问题,先留下这个问题return array_merge($selfConfig,$databaseConfig);//return $selfConfig;?>

外部引入的数据库链接文件和配置

<?phpreturn array(//链接数据库的方式:见DatabaseAction.class.php//主从数据库的配置(Common/convention.php)//1.开启数据库的分布式//    'DB_DEPLOY_TYPE'=> 1, // 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器)//2.必须要做数据库服务器中进行相应的配置//百度设置数据库集群//3.读写分离(默认是第一台服务器是写入服务器,其他的服务器的读服务器)//    'DB_RW_SEPARATE'=> true,// 数据库读写是否分离 主从式有效//ThinkPHP默认的字符集是utf8,不要加中划线- //'DB_FIELDTYPE_CHECK'=> false, // 是否进行字段类型检查//    'DB_FIELDS_CACHE'   => true,  // 启用字段缓存//    'DB_CHARSET'        => 'utf8',// 数据库编码默认采用utf8    //由于数据库的链接需要多个项目来使用可以在一个页面中定义个公共的配置项,返回一个array数组//ThinkPHP中的db目录:Lib/Think/Db/Db.class.php//连接数据库设置'DB_TYPE'=>'mysql','DB_HOST'=>'localhost',//设置主从数据时用//'DB_HOST'=>'localhost,192.168.123.1','DB_NAME'=>'thinkphp',//设置主从数据时若名字不同//'DB_NAME'=>'hibernate,ant,thinkphp','DB_USER'=>'root','DB_PWD'=>'root',//如果未修改可以不用填写'DB_POST'=>'3306','DB_PREFIX'=>'tb_',);?>

action

<?phpclass UserdbAction extends Action{public function index(){$user=M('User');$list=$user->select();$this->assign('title','thinkphp演示');$this->assign('alist',$list);$this->display();}public function add(){//D是需要些Model的,M不需要写$user=D('User');if ($vo=$user->create()){echo 'create成功';$user->password=md5($user->password);$user->createtime=time();//扩展函数需要进加载之后使用load('extend');$user->createip=get_client_ip();if ($user->add()){$this->success("用户注册成功");}else{$this->error($user->getError());}}else{echo 'create失败';$this->error($user->getError());}}public function del(){//D是需要些Model的,M不需要写$user=D('User');if ($vo=$user->delete($_GET['id'])){$this->success("用户删除成功");}else{$this->error($user->getError());}}public function edit(){$user=M('user');$id=$_GET['id'];$list=$user->where("id=$id")->find();$this->assign('user',$list);$this->assign('title','编辑用户');$this->display();}public function update(){$user=M('user');if ($vo=$user->create()){if ($lineNum=$user->save()){$this->success("用户更新成功");}else{$this->error($user->getError());}}else{$this->error($user->getError());}}}?>

model

<?phpclass UserModel extends Model{function modelTest(){echo '测试的跨模型操作,调用模型中的方法';}}?>

html:

index.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title><!--{$title}--></title></head><body><form action="__URL__/add" method="post">用户名:<input type="text" name="username">密码:<input type="text" name="password"><input type="submit" value="注册"><!--{__NOTOKEN__}--></form><voList name="alist" id="vo"><li><span>ID</span><!--{$vo['id']}--><span>用户名</span><!--{$vo['username']}--><span>IP</span><!--{$vo['createip']}--><a href="__URL__/del/id/<!--{$vo['id']}-->">删除</a><a href="__URL__/edit/id/<!--{$vo['id']}-->">编辑</a></li></voList></body></html>

edit.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title><!--{$title}--></title></head><body><form action="__URL__/update" method="post">用户名:<input type="text" name="username" value="<!--{$user['username']}-->"><br/>密码:<input type="text" name="password" value="<!--{$user['password']}-->"><br/>ip:<input type="text" name="createip" value="<!--{$user['createip']}-->"><br/>创建时间:<input type="text" name="createtime" value="<!--{$user['createtime']}-->"><br/><input type="submit" value="更新"><!--{__NOTOKEN__}--><input type="hidden" value="<!--{$user['id']}-->" name="id"></form></body></html>


原创粉丝点击