Ask me where the database class I wrote is wrong and cannot insert data & lt ;? Phpclass & nbsp; DB {private & nbsp; $ hostname; private & nbsp; $ username; private & nbsp; $ passwor asks me where the DB Class I wrote is wrong, no data inserted
Class DB {
Private $ hostname;
Private $ username;
Private $ password;
Private $ select_db;
Private $ con;
Private $ Error;
Public function _ construct ($ hostname, $ username, $ password, $ select_db ){
If (! Empty ($ hostname )&&! Empty ($ username )&&! Empty ($ select_db) // check whether the parameter is null. otherwise, no value is assigned.
{
$ This-> hostname = $ hostname;
$ This-> username = $ username;
$ This-> password = $ password;
$ This-> select_db = $ select_db;
$ This-> con = mysql_connect ($ this-> hostname, $ this-> username, $ this-> password );
If (! $ This-> con ){
$ This-> Error = die ('could Not Connect: '. mysql_error );
}
Else {
Mysql_select_db ($ this-> select_db, $ this-> con );
}
}
}
Public function _ destruct () {// end connection upon exit
Mysql_close ($ this-> con );
}
Public function insert ($ table, $ body) {// insert an array in table
$ Line1 = implode (',', $ body );
$ Line2 = implode (',', array_keys ($ body ));
Echo $ SQL = "INSERT INTO $ table ($ line2) VALUES ($ line1 )";
$ Result = mysql_query ($ SQL, $ this-> con );
If (! $ Result ){
Echo $ this-> Error;
Echo '20140901 ';
}
}
Public function update ($ table, $ body ){
}
Public function read ($ table, $ keyword ){
}
Public function delete ($ table, $ keyword ){
}
Public function getLastError () {// returns the last error message
Return $ this-> Error;
}
}
?>
The homepage calls
require('DB.class.php');
$DB = new DB('localhost','root','','dbtest');
$line1 = array(
'aa'=>'`aa`',
'bb'=>'`dd`'
);
$DB->insert('1234',$line1);
?>
Echo $ the SQL statement is INSERT INTO 1234 (aa, bb) VALUES ('A', 'DD ')
------ Solution --------------------
Require ('Db. class. php ');
$ DB = new DB ('localhost', 'root', '', 'dbtest ');
$ Line1 = array (
'A' => "'A '",
'BB' => "'DD '"
);
$ DB-> insert ('20140901', $ line1 );
------ Solution --------------------
Your $ line1 array is reversed.
The insert statement also has a format: insert into tb_member set username = "test", type = 1, lastlogindt = now (). Similar to the update style.
You can also look at the Database Class written by people, I feel very good: http://www.cnblogs.com/hooray/archive/2012/07/21/2603017.html