You can set the query result directly and save it. if there is a primary key value, it will be updated. if not, it will be inserted into the table.
/App/controllers/user_controller.php
Class UserController extends AppController {
Var $ uses = array ('user ');
Function index (){
}
Function login (){
If (! Empty ($ this-> data ['user']) {
// $ This-> user-> name = $ this-> data ['user'] ['name'];
$ User = $ this-> user-> find ("user. name = '". $ this-> data ['user'] ['name']. "'");
Print_r ($ user );
$ User = $ user ['user'];
Print_r ($ user );
If ($ user ['password'] = md5 ($ this-> data ['user'] ['password']) {
$ This-> Session-> write ('login', 1 );
$ This-> flash ('login suss !! ','/User ');
} Else {
If (! $ User ['id']) {
$ This-> flash ('No this user !! ','/User ');
} Else {
$ This-> flash ('name or pass is wrong !! ','/User ');
}
}
} Else {
$ This-> flash ('need name and password !! ','/User ');
}
Exit ();
}
Function logout (){
If ($ this-> Session-> check ('login ')){
$ This-> Session-> del ('login ');
}
$ This-> flash ('logout OK ','/user ');
Exit ();
}
Function reset (){
$ Name = 'aaa ';
$ Pass = 'aaa ';
$ User = $ this-> user-> findByName ($ name );
If ($ user ['user'] ['id']) {
$ This-> user-> set ($ user );
} Else {
$ This-> user-> set ('name', $ name );
$ This-> user-> set ('password', md5 ($ pass ));
}
$ Ret = $ this-> user-> save ();
If ($ ret ){
$ This-> flash ('update OK !! ','/User ');
} Else {
$ This-> flash ('update OK !! ','/User ');
}
}
}
?>
/App/models/user. php
Class User extends AppModel {
Var $ name = "user ";
Var $ useTable = 'users ';
}
?>
/App/views/user/index. view
Controller-> Session-> read ('login ')! = 1):?>
FormTag ('/user/login');?>
Input ('user/name');?>
Password ('user/password');?>
Submit ('submit ');?>
Simple user verification Trial $ model-> findByFields (); you can use any field in the model table to query
$ Model-> set ($ data );
$ Model-> set ($ key, $ value );
You can set the query result directly and save it. if there is a primary key value, it will be updated. if not, it will be inserted into the table.