thinkphp Implementing updated Data Example (demo)

Source: Internet
Author: User
This article introduces thinkphp implementation of the update data of the example and thinkphp update the database of five ways, this article is very good, with reference value, interested friends can refer to the next

Before the introduction of the text before the introduction of the data Update method to support the coherent operation method is:

In the previous article we implemented data deletion and bulk deletion, this article we will implement the data update.

  The first is still expected:  

Click Edit to go to the modi.html page , and then make changes, so that the real name of the property is modified:

Click Save:

The above is to achieve the effect, the following is the implementation of the specific code:

The first is still the code inside the view:

<form role= "form" method= "POST" action= "__module__/admin/user/doadd" ><p class= "Input-group" > <span class= "Input-group-addon" >  " Name: </span><input type=" text "class=" Form-control "placeholder=" "name=" username "></p><p class=" Input-group "> <span class=" Input-group-addon "for=" inputWarning1 "> real name: </span><input type=" text "class=" Form-control "placeholder = "id=" input "name=" Realname "></p><p class=" Input-group "> <span class=" Input-group-addon "> Phone Number: </span><input type= "text" class= "Form-control" placeholder= "" Name= "Telphone" ></p><p class= "Input-group" > <span class= "Input-group-addon" > E-mail: </span><input type= "text" class= " Form-control "placeholder=" "name=" email "></p><p class=" Input-group "> <span class=" Input-groUp-addon "> Add Time: </span><input type=" text "class=" Form-control "placeholder=" 2014-05-22 "Name=" Resgistertime "></p><p class=" Input-group "> <span class=" Input-group-addon "> Set Password: </span ><input type= "text" class= "Form-control" placeholder= "123456" name= "password" ></p><p class= " Input-group "> <span class=" input-group-addon "> Confirm password: </span><input type=" text "class=" Form-control "Placeholder=" 123456 "name=" Repassword "></p><p class=" Input-group "><button type=" Submit "class=" BTN btn-primary "> Bao  Save &LT;/BUTTON&GT;&L T;/p></form>

Next is the controller: what needs to be explained is that there is a need to write the value before the modification, mainly for the convenience of user memo, so need to make an if branch judgment:

Public Function Modi () {if (is_post) {$adminUsersModel = D ("Adminusers"); $adminUsersModel->create ();//Var_dump ($ Adminusersmodel);//Exit ("created successfully! if ($adminUsersModel->save ()) {$this->success ("modified successfully", U ("admin/user/lists"));} else {$this->error ($adminUsersModel->geterror ());}} else {$id = Isset ($_get[' id ')) intval ($_get[' id '): '; if ($id = = ") {exit (" bad param! Please enter the id ");} $adminUsersModel =d ("Adminusers"), $adminUsers = $adminUsersModel->find ($id);//var_dump ($adminUsers); $this Assign ("Adminusers", $adminUsers); $this->display ();}}

Here are the five ways to update the database under thinkphp

The first method:

$ model->where (' id=1 ')->save ($data);

The second method:

$ model->where (' id=1 ')->data ($data)->save ();

The third method:

$ model->create ();

$ model->save ();

The form must contain a hidden field with a primary key name

The fourth method:

$ model->where (' id=5 ')->setfield (' name ', ' thinkphp ');

$ model->where (' id=5 ')->setfield (Array (' name ', ' email '), array (' TP ', ' TP@163.com '));

The fourth method, when passing an array, I can not achieve ...

The Fifth method:

$ model->setinc (' score ', ' id=5 ', 3); Points plus 3

$ model->setinc (' score ', ' id=5 '); Points plus 1

$ model->setdec (' score ', ' id=5 ', 5); Points minus 5

$ model->setdec (' score ', ' id=5 '); Points minus 1

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.