Registration fo...LOGIN

Registration form submission and processing

Since it needs to be associated with the user table of the database to add it, TP5 has already done the processing internally and only needs to create a new application/index/model/User.php file

Write the code as follows:

<?php
namespace app\index\model;
use think\Model;
class User extends Model
{
   protected $pk = 'user_id';
}

As can be seen from the previous section, the path and method of registration submission are:

<form action ="/public/index.php/index/regist/regist" method="post">

##Continue editing application/index/controller/Regist.php

Write a regist method:

<?php
//用户注册
public function regist(){
  //实例化User
  $user = new User;
  //接收前端表单提交的数据
  $user->user_name = input('post.UserName');
  $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.UserBirth');
  $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('注册失败');
  }
}

$result will be equal to true only if all verifications pass. If there is an error, the corresponding string type error message will be returned.

The value obtained by input('post.UserName') is based on (submission method). (Nama value attribute of the form)

The registration function is completed in this way

The effect is shown as follows:

gif5新文件 (11).gif

The database is shown as follows:

微信图片_20180307134620.pngNext Section

<?php echo "注册功能实现";
submitReset Code
ChapterCourseware