ThinkPHP has a very strange problem with database operations. it cannot operate the database & lt ;? Phpclass & nbsp; TestAction & nbsp; extends & nbsp; Action & nbsp ;{& nbsp; & nbsp; function & nbsp; index () & nbsp; {ThinkPHP database operation problems
Very strange, unable to operate the database
Class TestAction extends Action {
Function index (){
$ Data = array ('username' => 'sunhu ',
'Email '=> 'asd ',
'Content' => 'Hello! '
);
$ List = M ("Guestbook")-> add ($ data );
If (! $ List ){
Echo 0;
} Else {
Echo 1;
}
}
}
?>
Browser output is 0
------ Solution --------------------
Reference:
Class TestAction extends Action {
Function index (){
$ Data = array ('username' => 'sunhu ',
'Email '=> 'asd ',
'Content' => 'Hello! '
);
$ Gusetbook = M (Guestbook );
$ Gus ......
Please do not mislead people! The $ Gusetbook-> create (); statement you wrote is unnecessary, because add is used to add data later.
Do you think the landlord writes this?
$ List = M ("Guestbook")-> add ($ data );
If (! $ List ){
Echo 0;
} Else {
Echo 1;
}
What I told you
$ Guestbook-> add ($ data); // There is only an insert operation, and no data is returned. if you want to prompt, you can write it like this.
If ($ Guestbook-> add ($ data );){
$ This-> success ("Operation successful ");
} Else {
$ This-> error ("Operation failed ");
}
Different ??
------ Solution --------------------
I personally think the problem may be the model instantiation.
If your custom model class is different from the standard configuration, for example, you can use $ trueTableName to define the table name. In this case, the M method may fail to instantiate the model normally.
You can first try to use the D method for instantiation