ThinkPHP学习笔记(八)一个用户增删改查的小例子
主要是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; ?>
外部引入的数据库链接文件和配置
<?php return 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
<?php class 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
<?php class UserModel extends Model{ function modelTest(){ echo '测试的跨模型操作,调用模型中的方法'; } } ?>
html:
index.html
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title><!--{$title}--></title>
edit.html
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title><!--{$title}--></title>

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Notepad++7.3.1
Easy-to-use and free code editor

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver CS6
Visual web development tools