How to implement database additions in THINKPHP5

Source: Internet
Author: User
Tags dsn
The face of a new frame installed generally do not know what to do. Let's do a write function first.

Get ready for work first, connect to the database first.

Config file in database.php under Application


return [  //Database type ' type  '      = ' mysql ',//  server address  ' hostname '    = ' 127.0.0.1 ',  //database name  ' database '    = ' shoptest ',//  username  ' username '    = ' root ',  //password  ' password '    = = ',  //Port  ' hostport ' + '    3306 ',  //Connect DSN  ' DSN '       = ',  // Database connection Parameters  ' params '     = [],  //Database encoding defaults to UTF8  ' charset '     = ' utf8 ',  //database table prefix  ' Prefix ' + '     ,  //Database debug mode  ' Debug ' +      true,  //Database deployment method: 0 Centralized (single server), 1 distributed (Master-slave server)  ' deploy ' +     0,  //database read and write whether separate master-slave effective  ' rw_separate ' and   false,  //read/write separation of the number of servers  ' Master_num ' +   1,  //Specify the server serial number  ' Slave_no '    and ' = ',  // Check whether the field exists  ' fields_strict '  = True,  //DataSet return type  ' resultset_type ' = ' array ',  // Auto-write timestamp field  ' auto_timestamp ' = False,  //Time field is removed after the default time format  ' datetime_format ' = ' y-m-d h:i:s ',  //Do you need to perform SQL performance Analysis  ' Sql_explain '   and false,];

Set it up and put it on first, set up a controller.

The default downloaded collection has an index folder in the application folder.

The inside is used to store the controller, class and template folder, the name can be modified.

If the use of 3.2 is very good understanding, is equivalent to the original home file.

Create three folders in this folder first

Then the Controller folder to create a new index.php file (in fact, there is a default).

Write such a code in class


Public Function Indexs () {    return ' I turned on the controller ';}

Enter the server name in the URL field/index/index/indexs

You can see a line of text.

Add the template first.

The use Think\view is added first, and the template library is introduced.


$view = new View;return $view->fetch (' template name ');

So where is the template placed. Controller folder has a view folder

If you drag the template file directly into the words will be an error.

Reading the second half of the wrong path. /application/index\view\index\adda.html

Application/index/view can find it all.

Next again is a folder name and file name.

This folder corresponds to the name of the controller, which means to create a corresponding folder. The template is then dragged in to invoke. Of course, a complete framework will not be the only one method. The other details are later in the study.

The two commonly used notation for form submissions, input (' post.xxx ') refers to getting the post value.


Call the Db class use think\db; $data = [' title ' = ' = ' = ' post.title '), ' content ' = ' post.title ')];D b::table (' Blogmsg ')->insert ($data);//establish model notation $mod = new \app\index\model\blogmsg; $mod->title = input (' Post.title '); $ Mod->content = input (' post.content '); $mod->save (); Echo $mod->id;

Comparatively speaking, the individual prefers this kind of wording. Use request to complete the addition. (Good for future use of Laravel)


Introduce request. Use Think\request;public function Add (Request $req) {  //If the added content needs to be processed, it will be taken separately and then assigned ($req->post (' title ') to get the value)  $a = RTrim ($req->post (' title '), ');  $req->post ([' title ', ' = ' $a]);  Ready to add  $mod = new \app\index\model\blogmsg;  Allowfield can choose which fields to add only.  $mod->allowfield ([' title ', ' content '])->save ($req->post ());  Gets the primary key returned by the  echo $mod->id;}

When you add data, you definitely need to verify it automatically.

This time the automatic verification can write a separate file. Create a validate folder within the index folder and create a PHP file with the same name inside the model folder. Easy to use.

Sample


Namespace App\index\validate;use think\validate;class blogmsg extends validate{  //write rule  protected $rule = [    ' Title ' = ' require|max:25 ',    ' content ' = ' require|max:255 ',  ];  Write error return information  protected $message = [    ' title.require ' = ' must be filled ', ' title.max ' = '    cannot exceed 25 characters ',    ' Content.require ' = ' must be filled ',    ' content.max ' = ' cannot exceed 255 characters ',  ];  protected $scene = ['    add ' = [' title ', ' content '],    ' edit ' = [' title ', ' content '],  ];}

Then the controller part of the change


$a = $mod->validate (True)->allowfield ([' title ', ' content '])->save ($req->post ()); if (false = = = $a) {  Validation failure output error message  dump ($mod->geterror ());  Die;}

Finally make a form token feature that prevents duplicate commits and remote submissions: Add {: Token ()} to the form form in the template, and then modify the rules to verify the rule plus a token. As follows:


' Title ' = ' Require|max:25|token ',

At this point, the article added a partial.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.