Automatic Verification and auto-filling are frequently used when thinkphp is used, but sometimes automatic verification and auto-filling do not work, this article analyzes the possible causes of thinkphp automatic verification and auto-filling failure and proposes corresponding solutions.
(1) There is a problem with the create () method
ThinkPHP automatic verification and automatic filling are implemented when the data object create () is created, so automatic verification and automatic filling are largely related to create.
The create method syntax is as follows:
Create (mixed data, string type)
Data indicates the accepted data, and type indicates the specific operation (write or update data ). Both parameters can be omitted. If the data parameter is omitted, $ _ POST data is accepted by default, and type is automatically recognized by the system by default.
However, the system automatically identifies type as a defect. When the input field has a primary key field, the system recognizes it as an update operation. Otherwise, it indicates a write operation. Therefore, when the primary key field is not automatically increased but needs to be written into SQL, automatic verification and automatic filling may be invalid.
For example, when adding a data record, if the form contains a primary key field or the system generates a primary key field (for example, enter the device number), ThinkPHP considers this operation as an update operation, for example, automatic verification and filling set below will be skipped:
Protected $ _ validate = array (
// Unique verification title when added
Array ('title', '', 'the title already exists! ', 0, 'unique', 1 ),
};
// Automatic Filling
Protected $ _ auto = array (
// Fill in the timestamp when adding the object
Array ('pubtime', 'time', 1, 'function '),
);
Although the add () operation is performed to write data to the data table, automatic verification and automatic filling are invalid.
In this case, you only need to explicitly pass the operation type into the create () method, that is, create ($ _ POST, 1), telling the system that this operation is writing data. In addition, if the input data is not $ _ POST, you must also pass the data as a parameter, such as create ($ _ GET ).
(2) The data field does not match
Due to carelessness, the form fields and data table fields are not properly matched.
(3) The data table fields have been changed.
During the development process, the table field name is changed, but the cache is not updated in time. As a result, the system determines that the field is invalid and is unset. Therefore, after the table field name is changed, clear the Data table cache in Runtime/Data.
(4) Incorrect Model name
The Model name is incorrect. It is not strictly named according to the specifications. For example, the first letter is not capitalized or careless, which leads to incorrect alphabetic order, many or fewer letters, etc. Such errors often cause model failure, especially in Case sensitivity.
(5) No auto-increment id for the data table
When the data table does not have an auto-increment id, the data to be verified cannot be found during verification, so it will fail.
The above are several possible reasons for ThinkPHP's automatic verification and Automatic Filling failure. When a problem occurs, you must exclude it one by one. I believe you can find the error, of course, there are still some unknown errors. If you find another reason, please leave a message below to correct it.
Articles you may be interested in
- How to Set automatic phpmyadmin login and cancel Automatic Login
- Php uses the filter function to verify the mailbox, url, and IP address
- PHP function for verifying that the ID card number is correct
- Javascript, php Regular Expression verification for mixed numbers and letters (6-15 digits)
- Debugging methods required for using ThinkPHP
- How to set the jump wait time for the thinkphp page Jump (successerror)
- Php extracts the birthday date from the ID card number and verifies whether it is a minor.
- Difference between execute and query methods in ThinkPHP