ThinkPHP-CURD add, delete, modify, and query operations

Source: Internet
Author: User
$ This-& gt; assign ('title', 'Add data'); // The template variable {$ title} if ($ demo-& gt; create () {// assign values to the autotime field: use date (& quot; Y-m-dH: I: s & quot ;) obtain the date format accepted by the datetime field type of the mysql database.

Adding, deleting, modifying, and querying CURD

First, a set of code CURDAction. class. php is provided.

// Test the addition, deletion, modification, and query operations on the database

ClassCURDActionextendsAction

{

// Index page

Publicfunctionindex ()

{

$ This-> assign ('title', 'Add data'); // The template variable {$ title} is set here}

$ This-> assign ('datetime', date ("Y-m-dH: I: s "));

$ This-> display ();

}

// INSERT operation

Publicfunctioncreate ()

{

// Initialize the ing object (O/RMapping) corresponding to the think_demo table)

$ Demo = D ("Demo ");

If ($ demo-> create ()){

// Assign values to the autotime field: obtain the date format accepted by the datetime field type in mysql database by date ("Y-m-dH: I: s.

$ Demo-> autotime = date ("Y-m-dH: I: s ");

// Insert operation

$ Demo-> add ();

// Jump to the display page

// $ This-> display ("read ");

$ This-> redirect ("read ");

} Else {

Header ("Content-Type: text/html; charset = utf-8 ");

Exit ($ demo-> getError (). '[return aaaaaa]');

}

}

// SELECT operation

Publicfunctionread ()

{

$ Demo = D ("Demo ");

$ Data = $ demo-> order ('iddesc')-> limit (10)-> select ();

$ This-> assign ('data', $ data );

$ This-> assign ('title', 'Add data 2 ');

$ This-> display ();

}

// UPDATE operation

Publicfunctionupdate ()

{

$ Demo = D ("Demo ");

// In practice, it is found that the create () method may not be executed if the user does not start the onClick event, because the database is not updated if the user clicks the page.

// Create () method: automatically extract data from html forms and inject it into Model objects.

// $ Demo-> create ();

// $ Demo-> save ();

// Same as the above code

$ Date ['title'] = $ _ POST ['title'];

$ Date ['content'] = $ _ POST ['content'];

$ Date ['id'] = $ _ POST ['id'];

$ Demo-> save ($ date );

$ This-> assign ('title', 'update data 2 ');

$ This-> display ();

}

// DELECT operation

Publicfunctiondelect ()

{

$ Demo = D ("Demo ");

$ Demo-> where ('Id = 5')-> delete ();

$ This-> redirect ("read ");

}

}

?>

After understanding the operation, you will be able to understand the above code. because it is necessary to get off work, I will go home and make a detailed explanation.

1. $ Demo = D ("Demo ");

Creates a ing object for a specified table through the database.

2. $ Demo-> create ()

Automatically loads the values in the form to the ing object using the same form name attribute name.

3. $ Demo-> add ();

Insert operations to insert data from non-empty ing objects into the database

4. $ Date ['title'] = $ _ POST ['title'];

$ Date ['content'] = $ _ POST ['content'];

$ Date ['id'] = $ _ POST ['id'];

$ Demo-> save ($ date );

UPDATE operation. here, unconditional update is performed, and the system will automatically update through the primary key.

5. $ Demo-> where ('Id = 5')-> delete ();

Delete an operation by setting conditions.

6. $ Data = $ demo-> order ('iddesc')-> limit (10)-> select ();

$ This-> assign ('data', $ data );

Conditional read operation, and then return the dataset to the V layer

No special instructions are for original articles. for reprinting, please note:Reposted from isCheck's blog _ focus on PHP development

Link:ThinkPHP-CURD add, delete, modify, and query operations

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.