Help with PDO (PREPARE)

Asked

Viewed 326 times

-1

Hello. I am trying to make a CRUD using PHP OO.

But when I run, I have an error in the preparation of the PDO class.

I have only the bank registration code and the bank connection code for now:

Banco.php

<?php

class Banco{

private $host = 'localhost', $usuario = 'root', $senha = '', $nomeBanco = 'rbtech', $conexao = null;

public function __construct(){
    $this->conecta();   // Chama metodo para conexao
}   //  Fim construct

public function __destruct() {
    if($this->conexao != null){
        $this->conexao = null;
    }   //  Fim destruct
}

    public function conecta(){
        try{
            $this->conexao = new PDO('mysql:host=' . $this->host . ';dbname=' . $this->nomeBanco, $this->usuario, $this->senha);
        } catch (PDOException $e) {
            die('Erro ao conectar com o banco' . $e->getMessage());
        }
    }   //  Fim conecta
}

Register.php

<?php

require_once '../Lib/Banco.php';

class Cadastro extends Banco{

// Porpriedades
public $nome, $sobrenome, $idade;

public function cadastrar(){
    if($_POST){ //  Verifica se é POST
        $pdo = parent::__construct();   //  Chama o construtor da classe Banco
        $sql = "INSERT INTO clientes (nome, sobrenome, idade) VALUES(:nome, :sobrenome, :idade)";   // Query INSERT
        $stmt = $pdo->prepare($sql);
        $stmt->bindParam(':nome', $nome);
        $stmt->bindParam(':sobrenome', $sobrenome);
        $stmt->bindParam(':idade', $idade);

        if($stmt->execute()){
            header('Location: ../index.php');
        }else{
            echo 'Erro ao cadastrar. ';
            print_r($stmt->errorInfo());
        }
    }
}   //  Fim cadastrar
}

$cad = new Cadastro();
$cad->nome = $_POST['nome'];
$cad->sobrenome = $_POST['sobre'];
$cad->idade = $_POST['idade'];
$cad->cadastrar();
var_dump($cad);

I’m starting my studies in Object Orientation with PHP, so this way of receiving form data with object orientation may not be the best way to do it, but a beginner in studies like me, that’s what I was able to develop. And as I said, the code when executed is error in prepare...

Fatal error: Call to a Member Function prepare() on null

Could someone help me with this problem?

And I accept suggestions on how to better receive data from an object-oriented form.

Grateful!

  • I think the problem may be in html..

  • Here is my HTML http://pastebin.com/5G1x2AWL

  • I looked over and couldn’t find any mistake in it

  • 1

    Constructor does not return value.

1 answer

2


In your example in your Bank class you need a Return in the constructor where you call the $this->connect() method and also a Return in the $this->connect() variable where you connect to the bank, I made the changes where you need to:

private $host = 'localhost', $usuario = 'root', $senha = '', $nomeBanco = 'rbtech', $conexao = null;

public function __construct(){
    return $this->conecta();   // Chama metodo para conexao
}   //  Fim construct

public function __destruct() {
    if($this->conexao != null){
        $this->conexao = null;
    }   //  Fim destruct
}

public function conecta(){
    try{
        return $this->conexao = new PDO('mysql:host=' . $this->host . ';dbname=' . $this->nomeBanco, $this->usuario, $this->senha);
    } catch (PDOException $e) {
        die('Erro ao conectar com o banco' . $e->getMessage());
    }
}   //  Fim conecta

Also edit the variables that you are feeding the bindParam, this way:

 $stmt->bindParam(':nome', $nome);
 $stmt->bindParam(':sobrenome', $sobrenome);
 $stmt->bindParam(':idade', $idade);

But you must edit to:

$stmt->bindParam(':nome', $this->nome);
$stmt->bindParam(':sobrenome', $this->sobrenome);
$stmt->bindParam(':idade', $this->idade);

For you are feeding these variables out of the method.

  • Thanks Robson, the bug in prepare has been solved. But now the code is giving error when checking on execute(). It returns false, and the message errorInfo() brings is that the name field cannot be null, but when I var_dump($Cad), in the object, the name variable is filled. I’m pretty sure that this is because I’m instantiating the class in the wrong way, can you tell me if that’s right? Could you help me? I couldn’t resolve :/ | Grateful!

  • Putz man, that was it, now it’s okay. I’m still used to accessing attributes like this kkk

Browser other questions tagged

You are not signed in. Login or sign up in order to post.