-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();
    }   
1

1 Answer 1

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();

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.