Home>Article>PHP Framework> How to use create method in thinkphp
In thinkphp, the create method is used to process the data submitted by POST, and automatically encapsulates the data instance using the corresponding relationship between the field names in the table and the names submitted by the form. This method can perform token verification on the form. Place the form for repeated submission.
The operating environment of this article: Windows 10 system, ThinkPHP version 5, Dell G3 computer.
1. The create method can process the data submitted by POST (automatically encapsulate the data instance through the corresponding relationship between the field name in the table and the name submitted by the form),
For example, there is a field named "username" in the user table. If there is anin the form, then
$User = M('User'); $data = $User->create(); echo $data['username'];
will Output "Xiao Ming", you don't need to use $_POST['username'] to receive it.
2. Use the create method to perform token verification on the form to prevent repeated submission of the form.
3. The data can be automatically verified. The premise is that you must manually create a UserModel.class.php file in the Model folder and add verification responsibilities in it.
protected $_validate = array( array('username','require','用户名必须', 1), );
4. The data can be automatically verified. The field is automatically assigned a value. The prerequisite is that you must manually create a UserModel.class.php file in the Model folder and add
protected $_auto = array( array('create_time','time',self::MODEL_INSERT,'function'), );
to it. Then the user's registration time will be automatically assigned the current time.
General In this case, for beginners who are just starting to learn, we will use a satisfactory method to add data to the database:
is to obtain each form through $_POST value. This method is not very efficient.
But thinkPHP provides us with a very convenient method called create. Before using this method, you must first set the name value in the form to be consistent with the value of the data field.
After the settings are completed, you can write code in the corresponding method in the controller. When executing this sentence, $data = $goods->create() will automatically obtain each value of the form. Even if you want to add fields to the data table in the future, you only need to add the corresponding field names to the form, which can be said to be very convenient.
if(IS_POST){ $goods = M('goods'); if($data = $goods->create()){ if($goods->add($data)){ $this->success('添加成功','show_list','2'); }else{ $this->error('添加失败'); }}}
The create method is also special - it can remove fields that are not in the database from the array.
For example, if there is no aa field in your database, but there is in your form, the create method will filter it out when submitting the added data.
Recommended study: "PHP Video Tutorial"
The above is the detailed content of How to use create method in thinkphp. For more information, please follow other related articles on the PHP Chinese website!