Trying to get Property of non-object

Asked

Viewed 955 times

-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>

that calls the log 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;

that sends to the Controlelogin class:

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");
    }
}

that checks for a compatible login and password in the bank by 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 send to the change page:

<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 changing the php parts within the value of inputs in the $Usu variables

I think the problem is in the DAO class that does not return any record and gives the error but I’m not sure

what I do?

  • In which line does the error occur? You can add this to the question?

  • I put it there

1 answer

0

To show the field you need to be inside 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 Property of non-object

  • In the same application I have a change for products and the same works without the use of foreach

  • I always had this error when I try to show the results of a select, it shows the same error "Trying to get..." because it needs to be inside a foreach, already if you take the data of the section do not need.... try with the foreach, let’s see

  • I tried, it didn’t help

  • why the "[0]" ($Usu[0]->name;) if you only have one session user? , it wouldn’t be $Usu->name;?

Browser other questions tagged

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