


Thinkphp implements automatic verification and automatic completion, _PHP tutorial
Thinkphp implements automatic verification and automatic completion.
Thinkphp’s automatic verification and automatic completion are based on the content submitted by the form. After rule verification and processing of some data Insert into database.
1. Automatic verification format:
array( array(验证字段1,验证规则,错误提示,[验证条件,附加规则,验证时间]), array(验证字段2,验证规则,错误提示,[验证条件,附加规则,验证时间]), ...... );
Verification conditions:
self::EXISTS_VALIDATE or 0, verify if the field exists (default)
self::MUST_VALIDATE or 1 must be verified
self::VALUE_VALIDATE or 2 Validate when the value is not empty
Verification time:
Self::MODEL_INSERT or 1 verify when adding new data
self::MODEL_UPDATE or 2 verify when editing data
self::MODEL_BOTH or 3 to verify in all cases (default)
2. Auto-complete format:
array( array(完成字段1,完成规则,[完成条件,附加规则]), array(完成字段2,完成规则,[完成条件,附加规则]), ...... );
Completion time:
self::MODEL_INSERT or 1 Processed when adding new data (default)
self::MODEL_UPDATE or 2 Processed when updating data
self::MODEL_BOTH or 3 All cases are handled
Small instance (registration)
HTML layout:
<form class="form-horizontal" action="{:U('Login/register')}" method="post" autocomplete="off" enctype="multipart/form-data"> <div class="form-group"> <label class="col-lg-2 control-label">用户名</label> <div class="col-lg-4"> <input class="form-control" type="text" name="username" /> </div> </div> <div class="form-group"> <label class="col-lg-2 control-label">密码</label> <div class="col-lg-4"> <input class="form-control" type="password" name="password" /> </div> </div> <div class="form-group"> <label class="col-lg-2 control-label">重复密码</label> <div class="col-lg-4"> <input class="form-control" type="password" name="repassword" /> </div> </div> <div class="form-group"> <label class="col-lg-2 control-label">Thinkphp implements automatic verification and automatic completion, _PHP tutorial</label> <div class="col-lg-4"> <input class="form-control" type="file" name="portrait" id="imgpath" /> <img src="/static/imghw/default1.png" data-src="http://www.bkjia.com/uploads/allimg/151228/1T1411363-1.jpg?2015111993855" class="lazy" style="max-width:90%" id="showimgpath" alt="Thinkphp implements automatic verification and automatic completion, _PHP tutorial"/> <span class="help-block">关像的大小为80*80px</span> </div> </div> <div class="form-group"> <label class="col-lg-2 control-label">性别</label> <div class="col-lg-4"> <div class="btn-group" data-toggle="buttons"> <label class="btn btn-default active"> <input type="radio" name="gender" autocomplete="off" value="1" checked /> 男 </label> <label class="btn btn-default"> <input type="radio" name="gender" autocomplete="off" value="0" /> 女 </label> </div> </div> </div> <div class="form-group"> <label class="col-lg-2 control-label">电话号码</label> <div class="col-lg-4"> <input class="form-control" type="input" name="phone" /> </div> </div> <div class="form-group"> <label class="col-lg-2 control-label">邮箱</label> <div class="col-lg-4"> <input class="form-control" type="input" name="email" /> </div> </div> <div class="form-group"> <div class="col-lg-2 col-lg-offset-2"> <button class="btn btn-primary btn-block btn-submit" type="submit">注册</button> </div> </div> </form>
Model (MemberModel)
<?php namespace Admin\Model; use Think\Model; class MemberModel extends Model { /* 自动验证 */ protected $_validate = array( array('username', '', '用户名是唯一的!', self::EXISTS_VALIDATE, 'unique', self::MODEL_INSERT), array('password', 'require', '没有填写密码!', self::EXISTS_VALIDATE, '', self::MODEL_INSERT), array('repassword', 'password', '重复密码不正确!', self::EXISTS_VALIDATE, 'confirm', self::MODEL_INSERT), array('phone','11','电话号码长度不对!', self::EXISTS_VALIDATE, 'length', self::MODEL_INSERT), array('email', 'email', '邮箱格式不正确!',self::EXISTS_VALIDATE, '', self::MODEL_INSERT) ); /* 自动完成 */ protected $_auto = array( array('password', 'encrypt', self::MODEL_INSERT, 'callback'), array('state','1',self::MODEL_INSERT), array('portrait', 'portrait', self::MODEL_INSERT, 'callback'), array('create_time', 'createTime', self::MODEL_INSERT, 'callback') ); /* 给密码加密 */ public function encrypt() { return md5(crypt(I('post.password/s'), 'zh')); } /* 创建时间 */ public function createTime() { return time(); } /* 上传Thinkphp implements automatic verification and automatic completion, _PHP tutorial */ public function portrait() { if($_FILES['portrait']['name']) { // 如果上传的Thinkphp implements automatic verification and automatic completion, _PHP tutorial $upload = new \Think\Upload();// 实例化上传类 $upload->maxSize = 3145728 ;// 设置附件上传大小 $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型 $upload->rootPath = './Uploads/portrait/'; // 设置附件上传根目录 // 上传单个文件 $info = $upload->uploadOne($_FILES['portrait']); if(!$info) {// 上传错误提示错误信息 $this->error($upload->getError()); }else{// 上传成功 获取上传文件信息 $portraitPath = './Uploads/portrait/'.$info['savepath'].$info['savename']; $image = new \Think\Image(); $image->open($portraitPath); // 生成一个居中裁剪为80*80的缩略图 $image->thumb(150, 150,\Think\Image::IMAGE_THUMB_CENTER)->save($portraitPath); return $info['savepath'].$info['savename']; } } } }
Corresponding data table structure:
Detect and insert into the database in the controller:
/* 注册 */ public function register() { if(IS_POST) { $member = D('member'); if($member->create()) { if($member->add()) { $this->success('注册成功!'); } else { $this->error('注册失败!'); } } else { exit($member->getError()); } } $this->display(); }
The above is the entire content of this article, I hope it will be helpful to everyone’s study
Articles you may be interested in:
- php form verification implementation code
- php session application instance login verification
- php cookie login verification sample code
- PHP verification code code (latest modification, fully customized!)
- php mobile phone number verification regular expression
- PHP code for session sharing and login verification through session id
- A beautiful php verification code class (share)
- PHP generates image verification codes, click to switch instances
- PHP uses CURL to simulate login to websites with verification codes Method

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

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 and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.
