host = DB_HOST; $this->user = DB_USER; $this->pwd = DB_PWD; $this->charset = CHARSET; $this->preFix = DB_PREFIX; $this->dbName = DB_NAME; if($tabName == ''){ $this->tabName = $this->prefix.strtolower(substr(get_class($this),0,-5)) ; }else{ $this->tabName = $this->preFix.$tabName; } $this->link = $this->connect(); } private function connect(){ $link = @mysqli_connect($this->host,$this->user,$this->pwd,$this->dbname) or die('数据库连接错误'); if(!$link){ return false; } mysqli_set_charset($link,$this->charset); return $link; } public function insert(array $data){ //var_dump($data); //INSERT INTO user(name,sex,age) VALUE(); $key = $val = ''; foreach($data as $k=>$v){ $key .='`'.$k.'`,'; $val .="'".$v."',"; } $key = rtrim($key,','); $val = rtrim($val,','); // var_dump($key); // var_dump($val); $sql = "INSERT INTO {$this->tabName} ({$key}) VALUES ({$val})"; echo $sql; return $this->exec($sql); } private function exec($sql){ $result = mysqli_query($this->link,$sql); if($result && mysqli_affected_rows($this->link) > 0){ return mysqli_insert_id($this->link) ?? mysqli_affected_rows($this->link); }else{ return false; } } } //调用方法,为什么不成功?总是插入不进去,提示false;?? '; // var_dump($m); $_POST = array('name'=>'小驴','age'=>'20','sex'=>'1'); $result = $m->insert($_POST); var_dump($result);
http://x80176m.cn/赫连芸芸资讯网声明
http://p42v77y.cn/令狐睿好资讯网支持
终于知道原因了!!已搞定!
按照代码应该是连接的问题,你可以尝试打印$link
看代码也没有问题,sql语句在数据库中也是可以正常执行的,但是调用类的插入方法之后,就不行了,这是怎么回事?