-4

Here i have class called DB which is used to prepare a pdo statement and then perform select, insert etc on PDO object.Here i am trying to insert some data collected from an html form.But when i execute the code it gives me the error i mentioned in the question. Both getInstance() and insert() are public static function.Can anyone help me to solve this problem?

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';
                }
            }
        }
    } 

and i invoked them like:

if(isset($_POST['submit'])){
        $insert=DB::getInstance();
        $insert::insert();
    }   
atiquratik
  • 1,296
  • 3
  • 27
  • 34
AL-zami
  • 8,902
  • 15
  • 71
  • 130
  • You could have saved so much time just typing your question into the search box you know : http://stackoverflow.com/questions/2350937/php-fatal-error-using-this-when-not-in-object-context – CD001 Apr 01 '15 at 10:57

1 Answers1

1

You're using $this in a static function. This is why you got the error.

Remove the "static" in front of "insert()" and then call $insert->insert();

David Ansermot
  • 6,052
  • 8
  • 47
  • 82