I want to ask my younger brother what is wrong with this DB Class I wrote and cannot insert data.

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

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.