I'm practicing Object Oriented PHP (OOP) and I'm developing a system for student enrollment, but when I try to enroll a student, the execute()
returns false
and I could not 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 registers with the bank. Sign Up.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 have already debugged the code and have seen that all bindValue
returns true
, but execute()
returns false
.