求问我自己写的这个DB类,哪里错了,插不进数据
<?php<br /> class DB{<br /> private $hostname;<br /> private $username;<br /> private $password;<br /> private $select_db;<br /> private $con;<br /> private $Error;<br /> <br /> public function __construct($hostname,$username,$password,$select_db){<br /> if(!empty($hostname)&&!empty($username)&&!empty($select_db))//检查参数是否为空,否则不赋值<br /> {<br /> $this->hostname = $hostname;<br /> $this->username = $username;<br /> $this->password = $password;<br /> $this->select_db = $select_db;<br /> $this->con = mysql_connect($this->hostname,$this->username,$this->password);<br /> if(!$this->con){<br /> $this->Error = die('Could Not Connect:'.mysql_error);<br /> }<br /> else{<br /> mysql_select_db($this->select_db,$this->con);<br /> }<br /> }<br /> }<br /> public function __destruct(){//退出时结束连接<br /> mysql_close($this->con);<br /> }<br /> public function insert($table,$body){//插入table中的一个数组<br /> $line1 = implode(',',$body);<br /> $line2 = implode(',',array_keys($body));<br /> echo $sql = "INSERT INTO $table ($line2) VALUES ($line1)";<br /> $result = mysql_query($sql,$this->con);<br /> if(!$result){<br /> echo $this->Error;<br /> echo '111';<br /> }<br /> <br /> }<br /> public function update($table,$body){<br /> <br /> }<br /> public function read($table,$keyword){<br /> <br /> }<br /> public function delete($table,$keyword){<br /> <br /> }<br /> public function getLastError(){//返回最后一条错误信息<br /> return $this->Error;<br /> }<br /> }<br />?>
主页调用的是
<?php<br />require('DB.class.php');<br /> $DB = new DB('localhost','root','','dbtest');<br /> $line1 = array(<br /> 'aa'=>'`aa`',<br /> 'bb'=>'`dd`'<br /> );<br /> $DB->insert('1234',$line1);<br />?>
echo $sql语句是INSERT INTO 1234 (aa,bb) VALUES (`aa`,`dd`)
------解决方案--------------------
require('DB.class.php');
$DB = new DB('localhost','root','','dbtest');
$line1 = array(
'aa'=> "'aa'",
'bb'=> "'dd'"
);
$DB->insert('1234',$line1);
------解决方案--------------------
你的$line1数组写反了。
而且insert 语句还有一种格式:insert into tb_member set username = "test", type = 1, lastlogindt = now()。跟update样式差不多。
你也可以看看人家写的数据库类,我感觉挺好的:http://www.cnblogs.com/hooray/archive/2012/07/21/2603017.html

TooptimizePHPcodeforreducedmemoryusageandexecutiontime,followthesesteps:1)Usereferencesinsteadofcopyinglargedatastructurestoreducememoryconsumption.2)LeveragePHP'sbuilt-infunctionslikearray_mapforfasterexecution.3)Implementcachingmechanisms,suchasAPC

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download
The most popular open source editor

SublimeText3 English version
Recommended: Win version, supports code prompts!

Notepad++7.3.1
Easy-to-use and free code editor
