Home Backend Development PHP Tutorial ThinkPHP学习札记(八)一个用户增删改查的小例子

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

Jun 13, 2016 pm 12:55 PM
gt lt php quot user

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;
?>
Copy after login

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

<?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_',
);
?>
Copy after login

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());
		}
	}
}
?>
Copy after login

model

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

html:

index.html



<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><!--{$title}--></title>


Copy after login
Copy after login
用户名: 密码:
  • ID 用户名 IP 删除 编辑

  • edit.html

    
    
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title><!--{$title}--></title>
    
    
    
    Copy after login
    Copy after login
    用户名:
    密码:
    ip:
    创建时间:


    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    AI Hentai Generator

    AI Hentai Generator

    Generate AI Hentai for free.

    Hot Article

    R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. Best Graphic Settings
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. How to Fix Audio if You Can't Hear Anyone
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    WWE 2K25: How To Unlock Everything In MyRise
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

    PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

    CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

    To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

    Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

    CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

    CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

    To work on file upload we are going to use the form helper. Here, is an example for file upload.

    CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

    Validator can be created by adding the following two lines in the controller.

    CakePHP Logging CakePHP Logging Sep 10, 2024 pm 05:26 PM

    Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

    How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

    Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

    CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

    CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

    See all articles