2017-05-05 233 views
-3

我已經創建了一個db類來處理數據庫。問題是,如果我想使用插入功能,它不會將用戶添加到數據庫。請幫忙!函數insert()不起作用PHP OOP

下面是代碼:

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 

和後續代碼var_dump($ FUS)從的index.php

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 
+0

http://php.net/manual/en/pdo.error-handling.php它是什麼?和http://php.net/manual/en/function.error-reporting.php –

+0

你試過打印出你的'$ this-> query() - > error()'? – hassan

+0

嘗試回顯查詢 –

回答

-2

感謝R.Smith。

我拼錯一個列名(birth_date沒有birth_day)

一切工作現在:)

感謝所有。