ThinkPHP implements detailed descriptions of data update instances (demo) and thinkphpdemo

Source: Internet
Author: User

ThinkPHP implements detailed descriptions of data update instances (demo) and thinkphpdemo

Before introducing the text, we will introduce the following consistent operations supported by the data update method:

In the previous article, we deleted and deleted data in batches. In this article, we will update the data.

  The first is expectation:

  

Click to enter the modi.html page after modificationAnd then modify the attributes of the real name:

Click Save:

The above is the implementation result. The specific code is as follows:

First, the code in the view is still:

<Form role = "form" method = "post" action = "_ MODULE _/Admin/User/doAdd"> <div class = "input-group"> <span class = "input-group-addon"> use  User  Name: </span> <input type = "text" class = "form-control" placeholder = "" name = "username"> </div> <div class = "input-group "> <span class =" input-group-addon "for =" inputWarning1 "> real name: </span> <input type = "text" class = "form-control" placeholder = "" id = "input" name = "realname"> </div> <div class = "input-group"> <span class = "input-group-addon"> mobile phone number: </span> <input type = "text" class = "form-control" placeholder = "" name = "telphone"> </div> <div class = "input-group "> <span class =" input-group-addon "> Email: </span> <input type = "text" class = "form-control" placeholder = "" name = "email"> </div> <div class = "input-group "> <span class =" input-group-addon "> Add time: </span> <input type = "text" class = "form-control" placeholder = "2014-05-22" name = "resgistertime"> </div> <div class = "input- group "> <span class =" input-group-addon "> set the password: </span> <input type = "text" class = "form-control" placeholder = "123456" name = "password"> </div> <div class = "input- group "> <span class =" input-group-addon "> confirm the password: </span> <input type = "text" class = "form-control" placeholder = "123456" name = "repassword"> </div> <div class = "input- group "> <button type =" submit "class =" btn-primary ">  Save </button> </div> </form>

Next is the Controller: It should be noted that the value before modification needs to be written here, mainly to facilitate user memo, so you 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! Enter id ") ;}$ adminUsersModel = D (" adminUsers "); $ adminUsers = $ adminUsersModel-> find ($ id); // var_dump ($ adminUsers ); $ this-> assign ("adminUsers", $ adminUsers); $ this-> display ();}}

Next, we will introduce five methods for thinkphp to update the database.

Method 1:

$ Model-> where ('Id = 1')-> save ($ data );

Method 2:

$ Model-> where ('Id = 1')-> data ($ data)-> save ();

Method 3:

$ Model-> create ();

$ Model-> save ();

The form must contain a hidden field named as the primary key.

Method 4:

$ Model-> where ('Id = 5')-> setField ('name', 'thinkphp ');

$ Model-> where ('Id = 5')-> setField (array ('name', 'email '), array ('tp ', 'tp @ 163.com '));

// Method 4: I cannot implement it when uploading arrays...

Method 5:

$ Model-> setInc ('score ', 'Id = 5', 3); // Add 3 points

$ Model-> setInc ('score ', 'Id = 5'); // Add 1 points

$ Model-> setDec ('score ', 'Id = 5', 5); // reduce points by 5

$ Model-> setDec ('score ', 'Id = 5'); // reduce points by 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.