效果:
驗證效果:
# 1.資料庫SQL腳本-- phpMyAdmin SQL Dump
-- version 4.4.15.5
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: 2016-07-09 13:12:42
-- 服务器版本: 5.7.12-log
-- PHP Version: 7.0.7
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
-- Database: `thinkphp5`
--
-- --------------------------------------------------------
--
-- 表的结构 `think_user`
--
CREATE TABLE IF NOT EXISTS `think_user` (
`user_id` int(11) NOT NULL,
`user_name` varchar(255) NOT NULL,
`user_sex` int(11) DEFAULT NULL,
`user_tel` varchar(255) DEFAULT NULL,
`user_email` varchar(255) DEFAULT NULL,
`user_address` varchar(255) DEFAULT NULL,
`user_birth` varchar(255) DEFAULT NULL,
`user_jointime` varchar(255) DEFAULT NULL,
`user_passwd` varchar(255) DEFAULT NULL,
`user_signature` varchar(255) DEFAULT NULL,
`user_hobby` varchar(255) DEFAULT NULL
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4;
--
-- 转存表中的数据 `think_user`
--
INSERT INTO `think_user` (`user_id`, `user_name`, `user_sex`, `user_tel`, `user_email`, `user_address`, `user_birth`,
`user_jointime`, `user_passwd`, `user_signature`, `user_hobby`) VALUES
(1, 'thinkphp', 1, '15700000000', 'emial@email.com', '山东省济南市****路', '1111111', '111111', 'qqq', NULL, NULL),
(2, 'pangPython', 1, '15700000000', 'email@emial.com', '山东省济南市', '201607096', NULL, '123456', 'nihao', '上速度速度');
--
-- Indexes for dumped tables
--
--
-- Indexes for table `think_user`
--
ALTER TABLE `think_user`
ADD PRIMARY KEY (`user_id`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `think_user`
--
ALTER TABLE `think_user`
MODIFY `user_id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=3;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
目錄結構:
#2 .寫入
檢視
controller/Regist.php<?php
namespace app\index\controller;
use think\View;
use think\Controller;
use app\index\model\User;
class Regist extends Controller{
public function index(){
$view = new View();
return $view->fetch('index');
}
//用户注册
public function regist(){
//实例化User
$user = new User;
//接收前端表单提交的数据
$user->user_name = input('post.user_name');
$user->user_sex = input('post.UserSex');
$user->user_tel = input('post.UserTel');
$user->user_email = input('post.UserEmail');
$user->user_address = input('post.UserAddress');
$user->user_birth = input('post.UserJoinTime');
$user->user_passwd = input('post.UserPasswd');
$user->user_signature = input('post.UserSignature');
$user->user_hobby = input('post.UserHobby');
//进行规则验证
$result = $this->validate(
[
'name' => $user->user_name,
'email' => $user->user_email,
'sex' => $user->user_sex,
'tel' => $user->user_tel,
'address' => $user->user_address,
'birth' => $user->user_birth,
'password' => $user->user_passwd,
],
[
'name' => 'require|max:10',
'email' => 'email',
'sex' => 'number|between:0,1',
'tel' => 'require',
'address' => 'require',
'birth' => 'require',
'password' => 'require',
]);
if (true !== $result) {
$this->error($result);
}
//写入数据库
if ($user->save()) {
return $this->success('注册成功');
} else {
return $this->success('注册失败');
}
}
}
繼承驗證類,但是PHP不支援多重繼承。需要自己選擇哪一種方式最適合。 細節方面,驗證規則可以自訂實作使用
驗證手機號,身分證號等。
model/User.php
<?phpnamespace app\index\model;use think\Model;class User extends Model{ protected $pk = 'user_id'; }
以上是ThinkPHP5開發(二)實作背景驗證的註冊功能的圖文程式碼詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!