函数insert()在PHP OOP中不起作用

函数insert()在PHP OOP中不起作用,php,mysql,function,oop,Php,Mysql,Function,Oop,我创建了一个db类来处理数据库。问题是,如果我想使用insert函数,它不会将用户添加到数据库中。请帮忙 代码如下: DB.php <?php class DB { private static $_instance = null; private $_pdo, $_query, $_error = false, $_results, $_count = 0; private function _

我创建了一个db类来处理数据库。问题是,如果我想使用insert函数,它不会将用户添加到数据库中。请帮忙

代码如下:

DB.php

<?php

class DB
{
    private static $_instance = null;
    private $_pdo,
        $_query,
        $_error = false,
        $_results,
        $_count = 0;

    private  function __construct ()
    {
        try
        {
            $host = config::get('mysql/host');
            $database = config::get('mysql/db');
            $username = config::get('mysql/username');
            $password = config::get('mysql/password');

            $this->_pdo = new PDO("mysql:host=$host;dbname=$database", $username, $password);

        }  catch (PDOException $e)
        {
            die($e->getMessage());
        }
    }

    public static function getInstance()
    {
        if(!isset(self::$_instance))
        {
            self::$_instance = new DB();
        }
        return self::$_instance;
    }

    public function query($sql,$params=array())
    {
        $this->_error = false;
        if($this->_query = $this->_pdo->prepare($sql))
        {
            $x = 1;
            if(count($params))
            {
                foreach($params as $param)
                {
                    $this->_query->bindValue($x,$param);
                    $x++;
                }
            }

            if($this->_query->execute())
            {
                $this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
                $this->_count = $this->_query->rowCount();
            } else
            {
                $this->_error = true;
            }
        }
        return $this ;
    }

    public function action($action , $table ,$where = array())
    {
        if(count($where) === 3)
        {
            $operators = array('=','>','<','>=','<=');

            $field = $where[0];
            $operator = $where[1];
            $value = $where[2];

            if(in_array($operator, $operators))
            {
                $sql = "{$action} FROM {$table} WHERE {$field} {$operator} ?";
                if(!$this->query($sql,array($value))->error())
                {
                    return $this;
                }
            }
        }
        return false ;
    }

    public function get($table , $where)
    {
        return $this->action("SELECT *",$table,$where);
    }

    public function delete($table , $where)
    {
        return $this->action('DELETE' ,$table , $where);
    }

    public function insert($table, $fields = array())
    {
        if (count($fields))
        {
            $keys = array_keys($fields);
            $values = '';
            $x = 1;

            foreach ($fields as $field)
            {
                $values .= '?';
                if ($x < count($fields))
                {
                    $values .= ', ';
                }
                $x++;
            }

            $sql = "INSERT INTO users (`" . implode('`, `', $keys) . "`) VALUES ({$values})";


            if (!$this->query($sql, $fields)->error())
            {
                return true;
            }
        }
        return false;
    }

    public function results()
    {
        return $this->_results;
    }

    public function first()
    {
        return $this->results()[0];
    }

    public function count()
    {
        return $this->_count;
    }

    public function error()
    {
        return $this->_error;
    }
}
和index.php中的var_dump($fus)

object(DB)[2]
  private '_pdo' => 
    object(PDO)[3]
  private '_query' => 
    object(PDOStatement)[4]
      public 'queryString' => string 'INSERT INTO users (`login`, `password`, `name`, `surname`, `birth_day`, `section`) VALUES (?, ?, ?, ?, ?, ?)' (length=108)
  private '_error' => boolean true
  private '_results' => null
  private '_count' => int 0
谢谢R.史密斯

我拼错了列名(出生日期无出生日期)

现在一切正常:)


谢谢大家。

结果如何?您是否已尝试打印出您的
$this->query()->error()
?尝试回显query您可以为我们显示MySQL表的模式吗?我还建议回显insert语句的值,并将其粘贴到GUI mysql工具中并使用该工具进行调试。我无法告诉您有多少次我输入了错误的类型,或拼写了错误的列名。
CREATE TABLE `users` (

`id` int(11) NOT NULL,

`login` varchar(50) NOT NULL,

`password` varchar(64) NOT NULL,

`name` varchar(50) NOT NULL,

`surname` varchar(50) NOT NULL,

`birth_date` date NOT NULL,

`section` enum('php','csharp','frontend','nosection') NOT NULL,

`groups_id` int(11) DEFAULT NULL
) 
ENGINE=InnoDB DEFAULT CHARSET=utf8
object(DB)[2]
  private '_pdo' => 
    object(PDO)[3]
  private '_query' => 
    object(PDOStatement)[4]
      public 'queryString' => string 'INSERT INTO users (`login`, `password`, `name`, `surname`, `birth_day`, `section`) VALUES (?, ?, ?, ?, ?, ?)' (length=108)
  private '_error' => boolean true
  private '_results' => null
  private '_count' => int 0