Traditional verification and thinkphp framework in php

Keywords: PHP SQL Database Javascript

PHP (hypertext preprocessor) can be used to build small websites. When users need to register and login, they need to match the background database to register and login. There are many traditional ways and steps. They need to connect the database first and insert sql statements.

<?php
header("Content-type: text/html; charset=utf-8");
$conn =mysqli_connect("localhost","root","");
if (!$conn){
      echo "<script>alert('Connection failed!');history.go(-1);</script>";
    } 
mysqli_select_db($conn,"liuyanban");
mysqli_query($conn,'SET NAMES utf8');
$password=$_POST['password'];
$username=$_POST['username'];
$face="yellow.png";
$result=mysqli_query($conn,"SELECT username from user1 where username = '$username'");  
$a=mysqli_num_rows($result);
if($a)
{       
      echo "<script language=javascript>alert('User name already exists!');location.href='reg.html'</script>";
}
else
{     
       $sql = mysqli_query($conn,"INSERT INTO user1(username,password,face)VALUES('1' ,'2','yellow.png')");
      if($sql)
      {
           echo "<script language=javascript>alert('login was successful!');location.href='login.html'</script>";
      }
      else
      {
            echo "<script>alert('Registration failed!');location.href='reg.html'</script>";
      }
}
?> 

The above is a native php registration example. It needs mysqli_select_db(), mysqli_query() and other functions to connect the database first. At the same time, only through mysqli_query() function can sql statements be executed. Finally, if statement can be used to classify and perform a series of other restrictive operations. In the native php stage, it is more practical, easy to understand, and the process is clear. But in a project, it is not easy to communicate with each other and very complicated to write such statement code. So it is necessary to build a project with thinkphp framework to make the coders dockable with each other, and also to facilitate the later code modification and function addition. So we don't need to elaborate the framework here, so we use controller (C) and model (M) to validate the form automatically in mvc mode under thinkphp framework:

Form static validation is used in the controller:

 public function doreg(){
              $data=D('user');
              $d=array();
                  $d['username']=$_POST['username'];
                  $d['password']=$_POST['password'];
                  $d['time']=date("Y-m-d H:i:s",time());
                  $d['qq']=$_POST['qq'];
                  $d['class']=$_POST['class'];
                  $mess=$data->create();
                  if (!$mess){       //Form automatic validation
                        $this->error($data->getError(),'Member/member',3);
                  }else{
                        $data->add();
                        echo "<script language=javascript>alert('login was successful!');location.href='member.html'</script>";
                      }
                  }

 

The template lists the fields that need to be validated:

<?php 
namespace Home\Model;
use Think\Model;
    class UserModel extends Model{
       protected $tableName ='user';     
        protected $_validate=array(                                  //Static verification
          //Array (Validation Field 1, Validation Rules, Error Tips, [Validation Conditions, Additional Rules, Validation Time])
            array('username','require','User name must be filled in!'),
            array('username','','The account name already exists!',0,'unique',1),
            array('repassword','password','The two passwords are inconsistent!',0,'confirm'),
            array('qq','require','qq Must fill in!'),
            array('qq','','The account name already exists!',0,'unique',1),
            array('class','require','Class must be filled in!'),
            array('j_verify','require','Verification code must!'),
        );
         
    }
?>

Here we take registration as an example, login is similar, if validation error, then use $this - > error ($data - > getError (),'Member / member', 3); form static validation is very convenient to use.

Posted by CAM on Sun, 23 Jun 2019 14:14:11 -0700