Key Points of curd application in Thinkphp
This article mainly introduces the practical points of the curd application in Thinkphp and attaches a simple example. It is a very good article. We recommend it to you here.
This is basically free to write down the specific application of curd for everyone. Of course, here we mainly talk about curd. What I am doing is adding, deleting, modifying, and querying users, without using the three major automatic
First
The Code is as follows:
Class IndexAction extends Action {
Public function index (){
Header ("Content-Type: text/html; charset = UTF-8 ″);
$ User = M ('user ');
$ List = $ user-> select ();
$ This-> assign ('user', $ list );
$ This-> display ();
}
Displays registration of all users on the homepage.
The Code is as follows:
Form action = "_ URL _/add" method = "post">
Username <input type = "text" name = "username">
Password <input type = "text" name = "password">
<Input type = "submit" value = "submit">
</Form>
<Volist name = "user" id = "vo">
Username: <input name = "username" value = "<{$ vo. username}>">
Password: <input name = "password" value = "<{$ vo. password}>">
Registered IP address: <input name = "cip" value = "<{$ vo. cip}>">
Registration Time: <input name = "ctime" value = "<{$ vo. ctime}>">
<A href = "_ URL _/del/id/<{$ vo. id}>"> Delete </a>
<A href = "_ URL _/edit/id/<{$ vo. id}>"> Update </a>
<Br>
</Volist>
Then our deletion method is very simple. The idea is that we can get the ID and delete the ID.
The Code is as follows:
If ($ user-> where ('$ _ GET ['id']')-> delete ())
{
$ This-> success ('deleted successfully ');
}
In this way, you can
How to add a user
The Code is as follows:
$ User = M ('user ');
If ($ user-> create ()){
$ User-> cip = get_client_ip ();
$ User-> ctime = time ();
$ User-> password = md5 ('Password ');
If ($ user-> add ($ data )){
$ This-> success ('user registration successful ','/admin. php/index/edit ');
} Else {
$ This-> error ($ user-> getError ());
}
} Else {
$ This-> error (getError ());
}
Update a user. We select a user based on the ID to output the user information.
The Code is as follows:
$ User = M ('user ');
$ Id = (int) $ _ GET ['id'];
$ User = M ('user ');
$ List = $ user-> where ("id = $ id")-> find ();
$ This-> assign ('LIST', $ list );
$ This-> display ();
Then, it is easier to update the user to save
The Code is as follows:
$ User = M ('user ');
If ($ user-> create ()){
$ User-> ctime = time ();
If ($ user-> save ()){
$ This-> success ('Update successful ');
}
} Else {
$ This-> error ('failed ');
}
This completes the addition, deletion, modification, and query of users. In fact, the simple function is what we add ourselves, for example
How many times can we log on to the forum? In fact, one setInc can solve the problem of logging on at a time + 1.
The number of times is enough.
Let's talk about it today.