php - using $this when not in object's context error -
this question has answer here:
here have class called db used prepare pdo statement , perform select
, insert
etc on pdo object.here trying insert
data collected html form.but when execute code gives me error mentioned in question. both getinstance()
, insert()
public static function.can me solve 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 $data){ $this->query->bindvalue($i,$data); } $i++; } public static function insert(){ $stmt='insert 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 invoked them like:
if(isset($_post['submit'])){ $insert=db::getinstance(); $insert::insert(); }
you're using $this in static function. why got error.
remove "static" in front of "insert()" , call $insert->insert();
Comments
Post a Comment