Problem inserting data into DB with PDO

0

My error:

Fatal error: Call to undefined method DB :: prepare () in C: \ xampp \ htdocs \ atTheClub \ adm \ classes \ houses.php on line 36

Files:

houses.php

class casas extends Crud{

%pr_e%

}

crud.php

require 'db.php'; abstract class Crud extends DB{ protected $table; abstract public function insert(); abstract public function update($idCasa); }

I deleted the delete and fetch functions to decrease the code snippet.

And below the file where you find the DB class that PHP says is the error: Home db.php require 'config.php'; class DB extends PDO{ private static $instance; public static function getInstance(){ if(!isset(self::$instance)){ try { self::$instance = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME, DB_USER, DB_PASS); self::$instance->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); self::$instance->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ); } catch (PDOException $e) { echo $e->getMessage(); } }; return self::$instance; }; public static function prepare($sql){ return self::getInstance()->prepare($sql); }; };

So, first I had not extended the PDO class to DB class, I thought this would solve, test and generate the same error, if anyone knows what is happening, thank you very much! I deleted all the checks, I left only what I was supposed to need to enter the data.

    
asked by anonymous 26.04.2016 / 15:56

2 answers

1

Your connection class is a bit confusing, I do not think there is a need to extend it to the PDO class because PDO is a native php class, so there is no need for the static prepare () function. try to make your class simpler perhaps;

class BD{   
    /**
     * [$pdo será objeto com a conexão]
     * @var [obj]
     */
    private static $pdo; 
    /**
     * [$host Host]
     * @var string
     */
    private static $host = 'seu_host';
    /**
     * [$name nome do banco]
     * @var string
     */
    private static $name = 'seu_banco';
    /**
     * [$charset charset utilizado no banco]
     * @var string
     */
    private static $charset = 'utf8';
    /**
     * [$user usuário do banco]
     * @var string
     */
    private static $user = 'usuario_do_banco';
    /**
     * [$passwd senha do banco]
     * @var string
     */
    private static $passwd = 'senha_do_usuario_do_banco';

    /**
     * [__construct Construtor da classe]
     */
    private function __construct(){}   

    /**
    * [getInstance Verifica se existe uma instância da conexão, caso nao exista retorna uma nova.]
    * @return [obj] [retorna objeto da conexão]
    */
    public static function getInstance() {   
        if (!isset(self::$pdo)):  
            try {   
                $opcoes = array();   
                self::$pdo = new \PDO("mysql:host=" .self::$host. "; dbname=" . self::$name . "; charset=" . self::$charset . ";", self::$user, self::$passwd, $opcoes);   
                self::$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);  
            } catch (PDOException $e) {   
                print "Erro com a conexão: " . $e->getMessage();   
            }   
        endif;   
        return self::$pdo;   
    }       
}  

In your class casas.php you can create a private object called $ pdo to be instantiated in the class constructor, making it available as the connection object for all its functions in the class.

require 'db.php';

protected $table = 'casas';
private $nomeCasa;
private $pessoaContatoCasa;
private $telefoneCasa;
private $emailCasa;
private $enderecoCasa;
private $pdo; // Será nossa conexão na classe

function __construct()
    {
        $this->pdo = BD::getInstance();// instancia conexão
    }

public function setNomeCasa($nomeCasa){
    $this->nomeCasa= $nomeCasa;
}

public function setPessoaContatoCasa($pessoaContatoCasa){
    $this->pessoaContatoCasa = $pessoaContatoCasa;
}

public function setTelefoneCasa($telefoneCasa){
    $this->telefoneCasa= $telefoneCasa;
}

public function setEmailCasa($emailCasa){
    $this->emailCasa= $emailCasa;
}

public function setEnderecoCasa($enderecoCasa){
    $this->enderecoCasa= $enderecoCasa;
}   

public function insert(){

    $sql = "INSERT INTO {$this->table} (nomeCasa, pessoaContatoCasa, telefoneCasa, emailCasa, enderecoCasa) VALUES (:nomeCasa, :pessoaContatoCasa, :telefoneCasa, :emailCasa, :enderecoCasa)";
    $stmt = $this->pdo->prepare($sql); 
    $stmt->bindParam(':nomeCasa', $this->nomeCasa);
    $stmt->bindParam(':pessoaContatoCasa', $this->pessoaContatoCasa);
    $stmt->bindParam(':telefoneCasa', $this->telefoneCasa);
    $stmt->bindParam(':emailCasa', $this->emailCasa);
    $stmt->bindParam(':enderecoCasa', $this->enderecoCasa);
    return $stmt->execute();
}
    
08.04.2017 / 05:02
0

To execute a method of a parent class use the parent

$stmt = parent::prepare($sql);
    
26.04.2016 / 16:53