Problems with PHP PDO

3

Next, I have the following method in a class named UsuarioDAO .

 public function insertUser(PojoUser $user) {

    if ($stmt = Conn::con() != null) {

        $stmt = Conn::con()->prepare('INSERT INTO usuario (email, username, password, lastaccess, active, createdat, updatedat) VALUES (:email, :username, :password, :lastaccess, :active, :createdat, :udatedat)');

        $stmt->bindValue(":email", $user->getEmail(), PDO::PARAM_STR);
        $stmt->bindValue(":username", $user->getUsername(), PDO::PARAM_STR);
        $stmt->bindValue(":password", $user->getPassword(), PDO::PARAM_STR);
        $stmt->bindValue(":lastaccess", $user->getLastAccess(), PDO::PARAM_STR);
        $stmt->bindValue(":active", $user->getActive(), PDO::PARAM_INT);
        $stmt->bindValue(":createdat", $user->getCreatedAt(), PDO::PARAM_STR);
        $stmt->bindValue(":udatedat", $user->getUpdatedAt(), PDO::PARAM_STR);

        $stmt->execute();

    } else {
        echo "<p class='msg-error'> Desculpe! Erro ao estabelecer conexão. Tente novamente mais tarde.</p>";
    }
}

This class takes as parameter an object of PojoUser , it follows the code of the class below:

<?php

class PojoUser {

    private $id;
    private $email;
    private $username;
    private $password;
    private $lastAccess;
    private $active;
    private $createdAt;
    private $updatedAt;

    public function getId() {
        return $this->id;
    }

    public function setId($id) {
        $this->id = $id;
    }

    public function getEmail() {
        return $this->email;
    }

    public function setEmail($email) {
        $this->email = $email;
    }

    public function getUsername() {
        return $this->username;
    }

    public function setUsername($username) {
        $this->username = $username;
    }

    public function getPassword() {
        return $this->password;
    }

    public function setPassword($senha) {
        $this->senha = $senha;
    }    

    public function getLastAccess() {
        return $this->lastAccess;
    }

    public function setLastAccess($lastAccess) {
        $this->lastAccess = $lastAccess;
    }

    public function getActive() {
        return $this->active;
    }

    public function setActive($active) {
        $this->active = $active;
    }

    public function getCreatedAt() {
        return $this->createdAt;
    }

    public function setCreatedAt($createdAt) {
        $this->createdAt = $createdAt;
    }

    public function getUpdatedAt() {
        return $this->createdAt;
    }

    public function setUpdatedAt($updatedAt) {
        $this->updatedAt = $updatedAt;
    }

}

?>

I call these two classes on the user registration page:

<?php

include_once('Register.class.php');
include_once('UserDao.class.php');
include_once('PojoUser.class.php');
if ($_POST) {

    $email = $_POST['user-email'];
    $password = $_POST['user-password'];
    $username = $_POST['user'];
    $conUserPassword = $_POST['conf-user-password'];

    /*$re = new Register();*/

    $user = new PojoUser();
    $userDAO = new UserDAO();
    $data = date('d/m/Y');


    if ($password == $conUserPassword) {

        if (!($userDAO->userExists($email))) {

            if ($userDAO->userNameAvaiable($username)) {

                $cryptoPassword = $userDAO->crypto($password);

                $user->setEmail($email);
                $user->setUsername($username);
                $user->setPassword($cryptoPassword);
                $user->setLastAccess($data);
                $user->setActive(0);
                $user->setCreatedAt($data);
                $user->setUpdatedAt($data);

                $userDAO->insertUser($user);



                //echo "<p class='msg-success'>Usuário Cadastrado com sucesso! Confirme seu email por favor.</p>";

            } else {
                echo "<p class='msg-error'>Desculpe, este nome de usuário não está mais disponível</p>";
            }

        } else {
            echo "<p class='msg-error'>Hey, parece que você já esteve por aqui, este email já está cadastrado em nosso sistema!</p>";
        }

    } else {

        echo "<p class='msg-error'>Verifique se você digitou as senhas corretamente!</p>";
    }

}

?>

My problem is that the user is never registered with the bank. Can anyone help me understand why?

    
asked by anonymous 27.08.2016 / 17:48

2 answers

1

Then the error was in the password setter of my class PojoUser :

private $id;
private $email;
private $username;
private $password;
private $lastAccess;
private $active;
private $createdAt;
private $updatedAt;

public function setPassword($senha) {
    $this->password = $senha;
}  

public function getPassword() {
    return $this->password;
}

I was not referencing the variable $password but rather a non-existent variable $senha , this made my password always go null for the insert method, which did not allow the database to be registered, because my getter returned password .

    
27.08.2016 / 21:01
0

The error is probably in this line:

if ($stmt = Conn::con() != null) {

The above expression evaluates whether Conn::con() is not null, and the result is a boolean value that is assigned the $stmt variable. Here's an example:

$foo = null;

$bar = ($foo != null); // False porque $foo NÃO é diferente de null
$baz = ($foo == null); // True porque $foo é igual a null

var_dump($bar, $baz);

The correct way is to first assign the $stmt the value of Conn::con() and then do the comparison:

if (($stmt = Conn::con()) != null) {

Why this is due to precedence of operators , more details: What is the difference between" & amp "and" || "and" and "and" or "in PHP? Which one to use?

    
27.08.2016 / 19:31