Error while saving data using PDO

Asked

Viewed 137 times

1

I’m having a problem entering data in the database using PDO and do not know the reason, in my view everything seems to be right, follow below the codes.

Model class

class User {
    private $id;
    private $nome;

    public setNome($nome){
        $this->nome = $nome;
    }

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

DAO class

class UsuarioDao implements Dao {
    private $conexao;  

    function __construct(){
        $connection = new Connection();
        $this->conexao = $connection->getConnection();
    }    

    public function insert($user){ 
        try {
            $query = "INSERT INTO usuarios(nome) VALUES(:nome)";
            $this->conexao->prepare($query);
            $this->conexao->bindValue(':nome',$user->getNome(),PDO::PARAM_STR);
            return $this->conexao->execute();    
        }catch(PDOException $e){
             echo $e->getMessage();
        }
    }

Instance of classes

require_once('User.class.php');
require_once('UserDao.class.php');

class Teste {
    $user = new User();
    $user->setNome('Gabriel');

    $userDao = new UserDao();
    $userDao->insert($user);         
}

About the error log no message appears, only the error message 500.

And I’m getting error 500 every time I run this code, I’d like someone to help me find the error in this code. I started learning PHP now I have more knowledge in JAVA taking advantage of the question you know some ORM framework like Hibernate for PHP ?

  • The first thing is to check the PHP and/or http server error log for the problem. Why put the User class if you don’t show the instantiation in the code?

  • It would be good to [Dit] the question and put all the relevant parts, including the instantiation. Anyway, it’s pretty certain that the answer to the problem lies in the server error log.

2 answers

1

I managed to resolve I made the following change :

    public function insert($user){ 
    try {
        $query = "INSERT INTO usuarios(nome) VALUES(:nome)";
        $resultado = $this->conexao->prepare($query);
        $resultado->bindValue(':nome',$user->getNome(),PDO::PARAM_STR);

        return $resultado->execute();    
    }catch(PDOException $e){
         echo $e->getMessage();
    }

I created the variable $result to receive the preparation of the query and it worked, thanks to colleagues who tried to help me.

-1

Change

class Teste {
    $user = new User();
    $user->setNome('Gabriel');

    $userDao = new UserDao();
    $userDao->insert($user);         
}

for

class Teste {
    $user = new User();
    $nome = $user->setNome('Gabriel');

    $userDao = new UserDao();
    $userDao->insert($nome);         
}

see if solve, I just entered a variable, $nome to receive $user->setNome('Gabriel');

Browser other questions tagged

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