Home > Backend Development > PHP Tutorial > php数据库插入数据失败解决办法

php数据库插入数据失败解决办法

WBOY
Release: 2016-06-13 12:07:43
Original
4575 people have browsed it

php数据库插入数据失败

本帖最后由 baidu_21003671 于 2014-09-20 20:19:25 编辑 class mysql{
 private $host;
 private $name;
 private $pass;
 private $table;
 private $ut;
 function __construct($host,$name,$pass,$table,$ut){
  $this->name=$name;
  $this->host=$host;
  $this->pass=$pass;
  $this->table=$table;
  $this->ut=$ut;
 
 
 }
function connect(){
if ($link=mysql_connect($this->host,$this->name,$this->pass)or die($this->error())){
/*echo "成功";*/
}//链接数据库
if(mysql_select_db($this->table,$link)or die("没该数据库".$this->table));
 /*echo "成功";*/
 mysql_query("SET NAMES'$this->ut'");
 
 
}
function query($v){
return  mysql_query($v);

}
function error(){
return mysql_error();

}
function close(){
return mysql_close();
}
/*---------------------*/
function fn_insert($table,$name,$value){
  if ($this->query("INSERT INTO $table ($name) VALUES ($value)")){
  echo "成功";
  }else {
  echo "失败";
  }

}
}
$a=new mysql('127.0.0.1','root','','','GBK');
$a->connect();
$a->fn_insert('test','id,user,tittle', '24,php800,测试');
$a->close()

语法没错误 链接数据库也正常 为什么就是插入数据就是失败呢


------解决思路----------------------
$a->fn_insert('test','id,user,tittle', "'24','php800','测试'");
Copy after login

------解决思路----------------------
$a=new mysql('127.0.0.1','root','123456','test','GBK');<br />$a->connect();<br />$a->fn_insert('test','id,user,title', "24,'php800','测试'");<br />$a->close();
Copy after login

检查检查先 
1.没有指定数据库
2.title应该拼写错误
3.varchar需要用‘’包裹
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template