Php beginners and thinkphp database writing problems

Source: Internet
Author: User
Php beginners, thinkphp write database problem configuration files
Return array (
'Db _ type' => 'mysql ',
'Db _ host' => 'localhost ',
'Db _ name' => 'test ',
'Db _ user' => 'root ',
'Db _ pwd' => '123 ',
'Db _ port' => '123 ',
'Db _ PREFX '=> '',
'Db _ FIELDTYPE_CHECK '=> false,
'Db _ FIELDS_CACHE '=> false,
'Db _ charset' => 'utf8'
);

Controller type
// This class is automatically generated by the system for testing purposes only
Class IndexAction extends Action {
Public function index (){
$ DataTest = new FirstModel ();
$ DataTest-> addData ();
$ Name = "hello thinkphp ";
$ This-> assign ($ name );
$ This-> display ();
}
}

?>

Model class
Class FirstModel extends Model {
Protected $ fields = array (
'Id', 'cname', 'Email ', 'indexs', 'btrue', 'created ',' _ PK' => 'id ', '_ autoinc' => true
);

Public function addData (){
$ Data = Array ();
$ Data ["cname"] = "aaa ";
$ Data ["email"] = "aaa@sina.com ";
$ Data ["indexs"] = 1;
$ Data ["btrue"] = 0;
$ Data ["created"] = time ();
$ First = M ('first ');
$ First-> create ($ data );
}
}
?>

Database name test, data table name first

However, after the operation, the data is not added to the database. please help me to see where the error is. thank you in advance.
I don't have much to offer.


Reply to discussion (solution)

$ DataTest-> addData (); does thinkphp use addData? Change to $ dataTest-> add ();

$ DataTest-> addData (); does thinkphp use addData? Change to $ dataTest-> add ();


AddData (). This method is written by myself in the model class. I also have it in the code provided on the first floor.

I also tried to write the method for adding a database directly in the action Controller class. instead of calling the method I wrote in the model class, I used add () and create (), but I cannot write it. I don't know why.


$ DataTest-> addData (); does thinkphp use addData? Change to $ dataTest-> add ();


AddData (). This method is written by myself in the model class. I also have it in the code provided on the first floor.

I also tried to write the method for adding a database directly in the action Controller class. instead of calling the method I wrote in the model class, I used add () and create (), but I cannot write it in. I don't know why create () is just to create data and verify, add () is to write to the database, in addDate () add $ first-> add ($ data );

$ First-> add ($ data), which I have tried and cannot be written.

$ First = new FirstModel ()

$ First = M ('first ')

$ First = D ('first ')

I have tried these instantiation methods.

$ First-> create ($ data)
$ First-> add ($ data)
These methods have been tried, but they cannot be written.

$ First-> add ($ data), which I have tried and cannot be written.

Add $ first-> add ($ data); followed by $ first-> getLastSql (); and send the printed SQL statement.

$firsr->data( $data )->add();


Only this

You can print the new SQL statement and paste the result.

Thanks to the help of friends upstairs, the problem has been found. according to the prompts of "fire53" and "zy205817", after printing the SQL statement, the prefix configuration in the configuration file is incorrect. Thanks again. paste it.

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.