Home  >  Q&A  >  body text

When inserting data encapsulated by the PHP class, the insertion is always unsuccessful and false is returned;

<?php
//数据库操作类
class Model{
    private $host; //数据库地址
    private $user; //数据库用户名
    private $pwd; //数据库密码
    private $tabName; //表名
    private $preFix; //表前缀
    private $dbName; //数据库名
    private $charset; //字符
    private $link=null; //数据连接对象

    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('数据库连接错误');
        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;??
<?php
$m = new Model('user');
// echo '<pre>';
// var_dump($m);

$_POST = array('name'=>'小驴','age'=>'20','sex'=>'1');
$result = $m->insert($_POST);

var_dump($result);

A _ Q _i_A _ Q _i_1060 days ago941

reply all(6)I'll reply

  • 小晴子

    小晴子2021-11-10 10:29:29

    http://x80176m.cn/Helian Yunyun Information Network Statement

    reply
    0
  • 小晴子

    小晴子2021-11-10 10:29:00

    http://p42v77y.cn/Linghu Ruihao Information Network Support

    reply
    0
  • A _ Q _i_

    A _ Q _i_2021-10-13 18:07:13

    Finally I know the reason! ! Done!

    reply
    1
  • autoload

    autoload2021-10-13 14:58:16

    image.png

    According to the code, it should be a connection problem. You can try printing $link

    reply
    0
  • A _ Q _i_

    The connection is successful, but the SQL statement cannot be executed.

    A _ Q _i_ · 2021-10-13 16:39:35
  • A _ Q _i_

    A _ Q _i_2021-10-13 10:22:50

    There is no problem when looking at the code. The sql statement can be executed normally in the database, but after calling the insert method of the class, it does not work. What is going on?

    reply
    0
  • Cancelreply