Simple operations for adding, deleting, modifying, and querying thinkphp

Source: Internet
Author: User
Add, delete, modify, and query operations on CURDAction. class. php & lt ;? Php // test the add, delete, modify, and query operations on the database classCURDActionextendsAction {// index page publ...

Adding, deleting, modifying, and querying CURD,First, a set of code CURDAction. class. php is provided.

  1.  
  2. // Test the addition, deletion, modification, and query operations on the database
  3.  
  4. ClassCURDActionextendsAction
  5.  
  6. {
  7.  
  8. // Index page
  9.  
  10. Publicfunctionindex ()
  11.  
  12. {
  13.  
  14. $ This-> assign ('title', 'Add data'); // The template variable {$ title} is set here}
  15.  
  16. $ This-> assign ('datetime', date ("Y-m-dH: I: s "));
  17.  
  18. $ This-> display ();
  19.  
  20. }
  21.  
  22. // INSERT operation
  23.  
  24. Publicfunctioncreate ()
  25.  
  26. {
  27.  
  28. // Initialize the ing object (O/RMapping) corresponding to the think_demo table)
  29.  
  30. $ Demo = D ("Demo ");
  31.  
  32. If ($ demo-> create ()){
  33.  
  34. // 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.
  35.  
  36. $ Demo-> autotime = date ("Y-m-dH: I: s ");
  37.  
  38. // Insert operation
  39.  
  40. $ Demo-> add ();
  41.  
  42. // Jump to the display page
  43.  
  44. // $ This-> display ("read ");
  45.  
  46. $ This-> redirect ("read ");
  47.  
  48. } Else {
  49.  
  50. Header ("Content-Type: text/html; charset = utf-8 ");
  51.  
  52. Exit ($ demo-> getError (). '[return aaaaaa]');
  53.  
  54. }
  55.  
  56. }
  57.  
  58. // SELECT operation
  59.  
  60. Publicfunctionread ()
  61.  
  62. {
  63.  
  64. $ Demo = D ("Demo ");
  65.  
  66. $ Data = $ demo-> order ('iddesc')-> limit (10)-> select ();
  67.  
  68. $ This-> assign ('data', $ data );
  69.  
  70. $ This-> assign ('title', 'Add data 2 ');
  71.  
  72. $ This-> display ();
  73.  
  74. }
  75.  
  76. // UPDATE operation
  77.  
  78. Publicfunctionupdate ()
  79.  
  80. {
  81.  
  82. $ Demo = D ("Demo ");
  83.  
  84. // 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.
  85.  
  86. // Create () method: automatically extract data from html forms and inject it into Model objects.
  87.  
  88. // $ Demo-> create ();
  89.  
  90. // $ Demo-> save ();
  91.  
  92. // Same as the above code
  93.  
  94. $ Date ['title'] = $ _ POST ['title'];
  95.  
  96. $ Date ['content'] = $ _ POST ['content'];
  97.  
  98. $ Date ['id'] = $ _ POST ['id'];
  99.  
  100. $ Demo-> save ($ date );
  101.  
  102. $ This-> assign ('title', 'update data 2 ');
  103.  
  104. $ This-> display ();
  105.  
  106. }
  107.  
  108. // DELECT operation
  109.  
  110. Publicfunctiondelect ()
  111.  
  112. {
  113.  
  114. $ Demo = D ("Demo ");
  115.  
  116. $ Demo-> where ('Id = 5')-> delete ();
  117.  
  118. $ This-> redirect ("read ");
  119.  
  120. }
  121.  
  122. }
  123.  
  124. ?>

After understanding the operation, you can understand the above code. because you have to get off work, I will go home and explain it again.

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 5 through the primary key. $ Demo-> where ('Id = 5')-> delete ();

Delete an operation by setting conditions.

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

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

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.