Execute() from PDO returns false

Asked

Viewed 868 times

1

I am practicing object-oriented PHP (POO) and I am developing a system for enrolling students, but when I try to register a student or execute() returns false and I couldn’t identify the problem.

Follow the code Student.php:

<?php

class Aluno
{
protected $nome;
protected $media;
protected $n1, $n2, $n3, $n4;

public function __construct($dados)
{
    $this->nome = $dados['nomeAluno'];
    $this->n1   = $dados['nota1'];
    $this->n2   = $dados['nota2'];
    $this->n3   = $dados['nota3'];
    $this->n4   = $dados['nota4'];
}

public function getNome()
{
    return $this->nome;
}

public function getN1()
{
    return $this->n1;
}

public function getN2()
{
    return $this->n2;
}

public function getN3()
{
    return $this->n3;
}

public function getN4()
{
    return $this->n4;
}
}

Now the code that makes the registration in the bank. Register.php:

<?php

require_once '../lib/Banco.php';
require_once 'Aluno.php';

class Cadastrar extends Aluno
{
    protected $aluno;
    public $dns = 'mysql:host=localhost;dbname';
    public $user = 'root';
    public $pass = 'vertrigo';

    public function __construct($aluno)
    {
        $this->aluno = new Aluno($aluno);
        $this->insert();
    }

    public function insert()
    {
        try {
            $pdo = new PDO($this->dns, $this->user, $this->pass);
        } catch (PDOException $e) {
            die('ERROOOOO' . $e->getMessage());
        }
        $insert = "INSERT INTO aluno(nome, nota1, nota2, nota3, nota4)
                    VALUES(:nome, :nota1, :nota2, :nota3, :nota4)";
        $stmt = $pdo->prepare($insert);
        $stmt->bindValue(':nome', $this->aluno->getNome()); //  result: true
        $stmt->bindValue(':nota1', $this->aluno->getN1());  //  result: true
        $stmt->bindValue(':nota2', $this->aluno->getN2());  //  result: true
        $stmt->bindValue(':nota3', $this->aluno->getN3());  //  result: true
        $stmt->bindValue(':nota4', $this->aluno->getN4());  //  result: true

        if ($stmt->execute()) { //  da maneira que esta o código execute() retorna false
            header('Location: ../index.php');
        } else {
            die('Erro ao cadastrar aluno');
            //var_dump($this->aluno->getNome());exit;
        }
    }
}

$cadastrar = new Cadastrar($_POST);

I’ve already debugged the code and seen that all bindValue return true, but the execute() returns false.

  • 1

    what is the name of the bank? public $dns = 'mysql:host=localhost;dbname'; place public $dns = 'mysql:host=localhost;dbname=seubanco';

  • Our guy, that’s right... me giving var_dump to everything that’s side and the problem was in this -_- Thanks for the help

  • You’re welcome! We’re here to help yourself. I added the answer

2 answers

4


Change that

public $dns = 'mysql:host=localhost;dbname'; 

for

 $dns = 'mysql:host=localhost;dbname=seubanco';

See if it solves.

  • Was that right :)

0

Check the value returned by $stmt->execute()

if ($stmt->execute()) {
    header('Location: ../index.php');
} else {
    var_dump($stmt->error);
    die;
}

Browser other questions tagged

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