In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to use the create method in thinkphp". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to use the 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 name in the table and the name of the form submission. This method can verify the form token and place the form to be submitted repeatedly.
This article operating environment: Windows10 system, ThinkPHP5 version, Dell G3 computer.
How to use create method in thinkphp
1. The create method can process the data submitted by POST (automatically encapsulate the data instance through the relationship between the field name in the table and the name submitted by the form)
For example, there is a field called "username" in the user table. If there is one in the form, then
$User = M ('User'); $data = $User- > create (); echo $data [' username']
It will output "Xiaoming" and you don't have to use $_ POST ['username'] to receive it.
2. The create method can be used to verify the token of the form and prevent the form from being submitted repeatedly.
3. You can validate the data automatically, provided that you manually create a UserModel.class.php file in the Model folder and add the verification responsibility to it.
Protected $_ validate = array (array ('username','require',' username must', 1),)
4. You can assign values to fields automatically, provided 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'),)
Then the registration time of user will be automatically assigned to the current time
Is to use $_ POST to get each form value. This method is not very efficient.
But thinkPHP provides us with a very convenient method of create. Before using this method, you must first set the value of the name in the form to be consistent with the value of the data field.
After the setup is complete, you can write code in the corresponding method in the controller. When you execute this sentence, $data = $goods- > create (), it automatically gets 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 (' add success', 'show_list','2');} else {$this- > error (' add failed');}
The create method is also special in that it removes 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 you submit the added data.
At this point, I believe you have a deeper understanding of "how to use the create method in thinkphp". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.