Home  >  Article  >  Backend Development  >  Detailed explanation of thinkPHP automatic verification, automatic addition and form error issues

Detailed explanation of thinkPHP automatic verification, automatic addition and form error issues

墨辰丷
墨辰丷Original
2018-05-31 16:20:541186browse

This article mainly introduces the analysis of thinkPHP automatic verification, automatic addition and form error problems, and analyzes the related usage skills and precautions of thinkPHP automatic completion mechanism in the form of a case. Friends in need can refer to it

I recently worked on another project and wanted to use thinkphp to write verification, but I cried for a few days. It was a token error at the beginning, and then something automatically added became invalid.

I have been testing and searching, and I found that the create() method originally had two parameters.

The first parameter is the data parameter that everyone knows, and the second parameter It is the hidden $type parameter. What is this parameter used to control? ?

Copy code The code is as follows:

//$type = $type?$type!empty($data[$this->getPk() ])?self::MODEL_UPDATE:self::MODEL_INSERT);

After thinking about this sentence carefully, I discovered that this hidden parameter is used to indicate the specific operation of this database?

$type takes a value of 1 for an insertion operation and 0 for an update operation. By default, there is no need to assign a value to this parameter because the system can automatically identify it.

If your primary key is automatically added by the database, then it will be fine. If you add it manually, it would be a tragedy. Because the default is to update data operation.

The following is the auto-complete code I wrote

protected $_auto = array(
array('password','sha1',1,'function'),
array('date', 'time', 1, 'function'),
);

Do you see that 1? That 1 means that when data is inserted, Just execute the function. PS: It is because of this that I have been struggling for a long time.

When you want to enter data, you can write like this:

create($_POST,1)//【插入数据】
create($_POST,2)//【更新数据】

Directly tell the create method that this operation is an insertion operation, which can solve the problem The auto-complete timing is wrong and the auto-complete is invalid.

But if you insist not to write it like this, I can't help it. You just need to remember that if the create() function is used, the default is to update the data. When writing the Model method.

thinkphp auto-complete diagram

Do you see it?

protected $_auto = array(
array('password','sha1',2,'function'),
array('date', 'time', 2, 'function'), //把1换成2就好了。
);

The problem is It can be solved.

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

Basic operations of php

Data types of PHP

PHP determines whether a file exists in the specified directory

PHP process signal processing

The above is the detailed content of Detailed explanation of thinkPHP automatic verification, automatic addition and form error issues. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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