PHP how to send array variables to Class function -


i'm writing code in php oop , i'm trying send $_post data filtered 1 class function class function add data database. login , password in registration form.

i have 3 classes that:

  1. is simple class handles connection database (i think not necessary put code here)
  2. is class filters coming $_post-s:

    class filter extends dbconnect { protected $login; protected $haslo;  public function regfilter() {     if (isset($_post))         {         foreach($_post $key => $val)             {             $filterval = strip_tags($val);             $filterval = htmlspecialchars($filterval);             $filterval = stripslashes($filterval);             $filterval = str_replace("\\", "", $filterval);             $filter = array(                 $key => $filterval             );             foreach($filter $key => $val)                 {                 echo "[$$key]";                 echo "$val";                 $                     {                     $key                     } = $val;                 }             }          return $filter = array(             'login' => $login,             'haslo' => $haslo         );         }       else         {         echo "proszę podać login hasło!";         }     } } 
  3. class login , password , send db:

    class dbinsert extends regfilter{ //protected $login; //protected $haslo; protected $query; protected $dbo; public function insert(){ //$this->extract = extract($filter); //$this->login = $login; //$this->haslo = $haslo; $this->connect(); $this->query = "insert uzytkownik (id, nazwa, haslo) values ('', :login, old_password(:haslo))";   if(!$result = $this->connect()->prepare($this->query)){ echo 'zapytanie nie powiodło się'; } else{ $result->bindparam(':login', $login); $result->bindparam(':haslo', $haslo); $login = $_post['login']; $haslo = $_post['haslo']; $result->execute(); } $dbo = null; } } 

now when try send data form objects:

    $rejestruj = new dbinsert();     $filtruj = $rejestruj->regfilter();     var_dump($filtruj);     $dodaj = $filtruj->insert();   

i following result:

   [$login]login      [$haslo]password123      array(2) { ["login"]= string(5) "login" ["haslo"]= string(11) "password123" }      fatal error: call member function insert() on array in `e:\xampp\htdocs\php\bazy_danych\obiektowe\my\register.php` on line 78 

which doesn't surprises me since: login , haslo returned "foreach" loop in class filter (which testing) "array(2)" returned "var_dump($filtruj);"(to check if working) , error returned since send array class dbinsert - in function put "extract" variables.

how can send variables filtered array class dbinsert?

edit: @twinfriends suggested corrected function insert in class dbinsert use prepared statement, thats why (for now) login , haslo variables reffering $_post. need answer question.

(first time posting, edit suggestions, advice appreciated since i'm quite beginner in php)

sorry took long answer, totally forgot question. well, lets take @ problem, hope solve it.

i try explain can, understand whats going on. first of all, lets @ error message

fatal error: call member function insert() on array in e:\xampp\htdocs\php\bazy_danych\obiektowe\my\register.php on line 78

okay. call function on array... lets have @ how call function:

$rejestruj = new dbinsert(); $filtruj = $rejestruj->regfilter(); var_dump($filtruj); $dodaj = $filtruj->insert();  

and here error. have understand call methods on objects , pass data methods, not call methods on data. mean that?

$rejestruj dbinsert object. create in first line of code here. then, call regfilter function on it. still fine. see, var_dump gives expected results. error has on last lane of code. , indeed, try call method insert() on array. , won't work, since array don't know method called insert().

the right call method (not final one!!!):

$dodaj = $rejestruj->insert();

now method call should work. in fact, won't insert anything. why? because insert() method try bind variables $login , $haslo - 2 variables method don't know. need pass data in method. that, have following changes:

method call:

$rejestruj->insert($filtruj); // $filtruj contains array

and dbinsert should like:

class dbinsert extends dbconnect{ protected $query; protected $dbo;      public function insert($data){          $this->connect();         $this->query = "insert uzytkownik (id, nazwa, haslo) values ('', :login, old_password(:haslo))";           if(!$result = $this->connect()->prepare($this->query)){             echo 'zapytanie nie powiodło się';         }         else {             $result->bindparam(':login', $data["login"]);             $result->bindparam(':haslo', $data["haslo"]);             $result->execute();         }         $dbo = null;     } } 

i hope code works changes. so, while in opinion code should work now, want mention there many things improve. example, you're not programming real "object-oriented" ... more pseudo oop you're writing here. things quite bad practice (could done easier). don't want dive deep details, since don't know if you're interested in it. if wish can give more advises, if wish.

otherwise hope answer you. if whole thing still doesn't work, please let me know can @ again.

have nice day ;)

edit:

since seems haven't been clear enough, here code how should now:

$rejestruj = new dbinsert(); $filtruj = $rejestruj->regfilter(); $dodaj = $rejestruj->insert($filtruj); 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -