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.