ThinkPHP database operations

Source: Internet
Author: User
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
Related Article

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.