I need help with the PHP language

Asked

Viewed 95 times

-2

So guys I’m having a hard time in a part of my code, as it is too big I will post only the part that is wrong:

<?php
   try{
    $entrar = $_POST["entrar"];
    $cpf = $_POST["cpf"];
    $senha = $_POST["senha"];
    /*Conexão com o banco de dados*/
    $pdo = new pdo('tipo_do_banco:host=servidor;dbname=nome_do_banco','nome_do_usuário','senha_do_usuario');
    $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
    /*Verificando se o usuário existe na tabela já*/
    var_dump($entrar);
    if($entrar==true){
    $validacao = $pdo->prepare('SELECT * FROM aluno WHERE cpf = :cpf AND senha = :senha LIMIT 1');
    $validacao ->bindValue('nome'); (não sei se está certo, queria pegar o nome do cara da tabela sql)
    $validacao ->bindParam(":cpf",$cpf,PDO::PARAM_STR);
    $validacao ->bindParam(":senha",$senha ,PDO::PARAM_STR);
    $validacao ->execute();
    /*Se a validação achar algo, vai direto para área do aluno*/
    $retorno = $validacao->FetchAll();
    if(count($retorno)>= 1){
        $_SESSION['nome'] = $nome;
        $_SESSION["cpf"] = $cpf;
        $_SESSION["senha"] = $senha;
        header('Location: alguma-area.php');
        exit();
    }else{
        unset ($_SESSION["cpf"]);
        unset ($_SESSION["senha"]);
        echo('Cpf ou senha inválidos');
    }
    }
    }catch(PDOException $error){
    echo 'Error: ' . $error->getMessage();
    }
?>

This IF does not execute, locking all other commands (has more code).

I would also like to know if it is possible through this select to get the name of the right table guy, how would it be done? Maybe using $validacao->bindValue('id');

  • 2

    Just before the if line do a var_dump($login) and post here the result

  • It was NULL when I did it, but I’m passing normally. Look at this... <input type="Submit" id="enter" name="enter" value="enter"> $enter = $_POST["enter"]; var_dump($enter); if($enter==true){

  • Do you declare $enter somewhere? maybe initialize it with false!

  • So there’s the problem.. edit the question showing the part of the code where you assign a value to the $enter variable

  • Yes, after the html form Ubmit, I open <?php and declare it $enter = $_POST["enter"];

  • check if the input of html is with the attribute name.

  • create a <input type="text" name="enter" value="Enter"> and delete the attribute name of the button, then in PHP make $enter = isset($_POST['enter'])

  • Well, it gave the bool value (false, for not being clicked), but lost the Submit to turn true and send the information...

  • Before if(Count... puts print_r($validacao->errorInfo())

  • Substitution new pdo for new PDO

  • -advantisaam, I did this, but it does not understand the if function at the time of giving TRUE and does not return error or anything

  • Before that line $retorno = $validacao->FetchAll(); give print_r($validacao->errorInfo())

  • 1

    Guys, thanks for your help, I’ll close the topic =)

Show 8 more comments

1 answer

1


I changed some things that were wrong in your code, try it this way I put down.

<?php


//error_reporting(E_ALL);
//ini_set('display_errors', 1);

if (session_status() !== PHP_SESSION_ACTIVE) {
    session_start();
}

if ($_SERVER['REQUEST_METHOD'] == "POST") {

    $entrar = $_POST["entrar"];
    $cpf = filter_var($_POST["cpf"], FILTER_SANITIZE_SPECIAL_CHARS);
    $senha = filter_var($_POST["senha"], FILTER_SANITIZE_SPECIAL_CHARS);
    $senha = md5($senha);

    try{
        $pdo = new \PDO('mysql:host=127.0.0.1;dbname=banco;port=3306','user','pass');
        $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);

        if(isset($cpf) && isset($senha)){
            $validacao = $pdo->prepare('SELECT * FROM aluno WHERE cpf = :cpf AND senha = :senha LIMIT 1');
            $validacao->bindParam(":cpf", $cpf, PDO::PARAM_STR, 20);
            $validacao->bindParam(":senha", $senha, PDO::PARAM_STR, 255);
            $validacao->execute();
            $retorno = $validacao->fetch(PDO::FETCH_ASSOC);
        }
    }catch(PDOException $error){
        echo 'Error: ' . $error->getMessage();
    }

    if(count($retorno) >= 1){
        $_SESSION['nome'] = $retorno['nome'];
        $_SESSION['cpf'] = $retorno['cpf'];
        $_SESSION['senha'] = $retorno['senha'];
        header('Location: alguma-area.php');
        exit();
    }else{
        echo('Cpf ou senha inválidos');
    }
}
?>

Browser other questions tagged

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