Basic ThinkPHP CURD operations, thinkphpcurd

Source: Internet
Author: User

Basic ThinkPHP CURD operations, thinkphpcurd

Speaking of CURD, anyone familiar with SQL knows that it is the addition, deletion, modification, and query. This CURD is often inseparable from business systems. Recently, ThinkPHP has just been used, thinkPHP is more flexible than native PHP. Next I will briefly introduce my learning experience.

To learn how ThinkPHP operates on MySQL, you must first have MySQL and then the runtime environment of PHP.

Wamp can help you solve the trouble of configuration. There are a lot of wamp information, Baidu is enough.

Next we will briefly introduce the process of adding, deleting, modifying, and querying ThinkPHP.

1. Create a database and name it t_user.

Code:

     

 DROP TABLE IF EXISTS `t_user`;      CREATE TABLE `t_user` (      `userid` int(11) NOT NULL,      `username` varchar(25) DEFAULT NULL,      `usersex` varchar(6) DEFAULT NULL,      `userage` int(6) DEFAULT NULL,      PRIMARY KEY (`userid`)      ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Create a project named thinkPHP_Text and import the thinkphp core package.

Configure the index. php file.

Start the project and automatically generate the directory. For example:

2. add () operation on thinkphp.

Create the index action file named IndexController. class. php and write a function insertUser (). In the control layer, you need to get the value of the foreground.

/*** Add User information * encoding time: */public function insertUser ($ id, $ name, $ sex, $ age) {$ this-> db (1, "DB_CONFIG1")-> db (1); $ condition = array (// defines the data to be added, put it in an array, name it $ condition 'userid' => $ id, 'username' => $ name, 'usersex '=> $ sex, 'userage' => $ age ,); $ addInfo = $ this-> db (1, "DB_CONFIG1")-> add ($ condition); // run the SQL statement, insertif ($ addInfo) {header ("Location: http: // localhost/thinkPHP_Text/index. php ");} echo $ this-> getLastSql (); // for debugging, the output SQL statement return $ addInfo ;}/**

In the model layer, remember the naming method. In this configuration, name UserModel. class. php, corresponding:

1/** 2 * Add User Information 3 * encoding time: 2015-05-28 4 */5 public function insertUser ($ id, $ name, $ sex, $ age) {6 $ this-> db (1, "DB_CONFIG1")-> db (1); 7 $ condition = array (// defines the data to be added, put it in an array, name $ condition 8 'userid' => $ id, 9 'username' => $ name, 10 'usersex '=> $ sex, 11 'userage' => $ age, 12); 13 $ addInfo = $ this-> db (1, "DB_CONFIG1")-> add ($ condition); // execute the SQL statement, insert14 if ($ addInfo) {15 header ("Location: http: // localhost/thinkPHP_Text/index. php "); 16} 17 echo $ this-> getLastSql (); // for debugging, the output SQL statement 18 return $ addInfo; 19}

This is the core code of the add operation.

For more information about the code, see the following link:

Http://pan.baidu.com/s/1hq7wfnm

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.