<?php //數(shù)據(jù)庫(kù)操作類(lèi) class Model{ private $host; //數(shù)據(jù)庫(kù)地址 private $user; //數(shù)據(jù)庫(kù)用戶(hù)名 private $pwd; //數(shù)據(jù)庫(kù)密碼 private $tabName; //表名 private $preFix; //表前綴 private $dbName; //數(shù)據(jù)庫(kù)名 private $charset; //字符 private $link=null; //數(shù)據(jù)連接對(duì)象 function __construct($tabName = ''){ $this->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('數(shù)據(jù)庫(kù)連接錯(cuò)誤'); 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; } } } //調(diào)用方法,為什么不成功?總是插入不進(jìn)去,提示false;?? <?php $m = new Model('user'); // echo '<pre>'; // var_dump($m); $_POST = array('name'=>'小驢','age'=>'20','sex'=>'1'); $result = $m->insert($_POST); var_dump($result);
http://x80176m.cn/Helian Yunyun Déclaration du réseau d'information
http://p42v77y.cn/Linghu Ruihao Assistance au réseau d'information
D'après le code, il devrait s'agir d'un problème de connexion. Vous pouvez essayer d'imprimer $link
.Il n'y a aucun problème en regardant le code. Les instructions SQL peuvent être exécutées normalement dans la base de données, mais après avoir appelé la méthode insert de la classe, cela ne fonctionne pas.