函数insert()不起作用PHP OOP

时间:2017-05-05 12:55:07

标签: php mysql function oop

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

以下是代码:

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

<?php

require_once 'core/init.php';

$fus = DB::getInstance();
$fus->insert('users', array(
    'login' => 'Speedy',
    'password' => 'pass123',
    'name' => 'Jake',
    'surname' => 'Will',
    'birth_day' => '1986-05-04',
    'section' => 'php',
));

请帮忙! :)

这是我的表结构:

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
来自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

1 个答案:

答案 0 :(得分:-2)

谢谢R.Smith。

我拼错了一个列名(birth_date no birth_day)

现在一切正常:)

谢谢大家。