Trying to get property of non-object

-1

I have this form:

<form class="login" name="login" method="post" action="../controle/usuario-controle.php?op=logar">

    <input class="input" type="text" name="txtlogin" pattern="^([0-9a-zA-Z]+([_.-]?[0-9a-zA-Z]+)*@[0-9a-zA-Z]+[0-9,a-z,A-Z,.,-]*(.){1}[a-zA-Z]{2,4})+$" required="required" placeholder="Email">
    <input class="input" type="password" name="txtsenha" pattern="^[0-9A-ú´~'^¨°ºª!@#$%&*-_=+.,<>;:/?()\|\\[\]\{\}]{7,15}$" required="required" placeholder="Senha">

    <input class="submit" type="submit" name="btnlogar" id="btnlogar" value="Conectar">
    <a href="cadastrar-usuario.php"><button type="button" class="btn btn-link"><span>Cadastrar-se</span></button></a>
</form>

calling the logar method:

case 'logar':
if (isset($_POST['txtlogin']) &&
        isset($_POST['txtsenha'])) {

    $cont = 0;
    if (!Validacao::validarLogin($_POST['txtlogin'])) {
        $cont++;
    }
    if (!Validacao::validarSenha($_POST['txtsenha'])) {
        $cont++;
    }

    if ($cont == 0) {
        $login = Validacao::retirarEspacos($_POST['txtlogin']);
        $login = Validacao::retirarAspas($login);

        $senha = Validacao::retirarEspacos($_POST['txtsenha']);
        $senha = Validacao::retirarAspas($senha);

        $usuario = new Usuario();
        $usuario->login = $login;
        $usuario->senha = $senha;
        ControleLogin::logar($usuario);
    } else {
        $_SESSION['msg'] = 'Campo usuário e/ou senha em branco!';
        header('location:../visao/resposta.php');
    }
} else {
    echo 'Não existe usuario e/ou senha!';
}
break;

which sends to the class ControlLogin:

public static function logar($u) {
    $uDAO = new UsuarioDAO();
    $usuario = $uDAO->verificarUsuario($u);

    if ($usuario && !is_null($usuario)) {
        //Mando o usuário para a página desejada
        $_SESSION['Usuario'] = serialize($usuario);
        header('location:../index.php');
    } else {
        $erros = "Email ou senha inválidos";
        $_SESSION['erro'] = serialize($erros);
        header("location:../visao/erro.php");
    }
}

which checks to see if there is a compatible login and password in the database for the DAO class:

public function verificarUsuario($u) {
    try {
        //Inseri o comando sql na variável stat
        $stat = $this->conexao->prepare("select * from usuario where login = ? and senha = ?");

        //Coloca as variáveis nas ?
        $stat->bindValue(1, $u->login);
        $stat->bindValue(2, $u->senha);

        //Inseri o resultado do select na variável usuario
        $usuario = $stat->fetchObject('Usuario');

        $this->conexao = null;

        //Retorna a variável usuario
        return $usuario;
    } catch (PDOException $e) {
        //Menssagem de erro
        echo 'Erro ao verificar usuário!';
    }
}

and sends to the page change:

<form name="cad" method="post" action="../controle/usuario-controle.php?op=confirmar-alterar">
    <?php
    if(isset($_SESSION['Usuario'])) {
        include_once '../modelo/usuario.class.php';
        $usu = unserialize($_SESSION['Usuario']);
    ?>
    <input type="text" name="txtnome" value="<?php echo $usu[0]->nome; ?>" pattern="^[A-zÁ-ú]{2}[A-zÁ-ú ]{0,23}$" required="required">
    <input type="text" name="txtlogin" value="<?php echo $usu[0]->login; ?>" pattern="^{7,20}$" required="required">
    <input type="password" name="txtsenha" value="<?php echo $usu[0]->senha; ?>" pattern="^[0-9A-ú´~'^¨°ºª!@#$%&*-_=+.,<>;:/?()\|\\[\]\{\}]{7,15}$" required="required">

    <input class="submit" type="submit" name="btnalterar" value="Alterar">
    <?php 
    } 
    ?>
</form>

The error is in the change in the parts of php within the value of the inputs in the variables $ usu

I think the problem is in the DAO class that returns no record and gives the error but I'm not sure

What do I do?

    
asked by anonymous 18.10.2017 / 21:37

1 answer

0

To show the field must be within a "foreach"

example:

<?php foreach($resultadodaBuscanoBanco as $resultado){
?>
<input type="text" name="txtnome" value="<?php echo resultado->login?>"required">
<?php}
?>

OBS without it shows the error of Trying to get non-object property

    
18.10.2017 / 22:51