2015-04-01 23 views
-4

在這裏,我有一個名爲DB類,這是用於製備PDO語句,然後在PDO object.Here執行selectinsert等我試圖從insert HTML表單收集了一些數據但是當我執行代碼時,它給了我在問題中提到的錯誤。 getInstance()insert()都是公共靜態函數。任何人都可以幫助我解決這個問題嗎?

class DB{ 
     private static $_instance=null; 
     private $pdo, 
       $query, 
       $error=false, 
       $results, 
       $count=0; 
     private function __construct(){ 
       try{ 

        $this->pdo=new PDO('mysql:host='.Config::get('mysql/host').';dbname='.Config::get('mysql/db'),Config::get('mysql/user'),Config::get('mysql/password')); 


       }catch(PDOException $e){ 
        echo $e->getMessage(); 
       } 
     } 
     public static function getInstance(){ 
      if(!isset(self::$_instance)){ 
        self::$_instance=new DB(); 
      } 
      return self::$_instance; 
     } 

     private function bind($val){ 
       $i=1; 
       foreach($val as $data){ 

        $this->query->bindValue($i,$data); 

       } 
       $i++; 

     } 
     public static function insert(){ 
      $stmt='INSERT INTO users (username,password,name) VALUES (?,?,?)'; 
      if($this->query=$this->pdo->prepare($stmt)){ 
       $val=Validate::send(); 
       $this->bind($val); 
       if($this->query->execute()){ 

        return 'successfull'; 
       } 
      } 
     } 
    } 

,我調用它們,如:

if(isset($_POST['submit'])){ 
     $insert=DB::getInstance(); 
     $insert::insert(); 
    } 
+0

你可以保存這麼多的時間裏直接鍵入你的問題到你知道的搜索框:http://stackoverflow.com/問題/ 2350937/PHP的致命錯誤 - 使用 - 這 - 時 - 不 - 在對象上下文 – CD001 2015-04-01 10:57:53

回答

1

您使用$這在靜態函數。這就是你得到錯誤的原因。

前面去掉「靜」,「插入()」,然後調用$insert->insert();

相關問題