Problems with PDO

1

I'm new to PHP and would like to ask a question:

In the code I will show below, I tested the query in the database up to try , the problem is that after try(){} prepare it does not return anything, I tested it with die(''); and it only returns me the echo and only goes there.

Follow the login.php code

<?php
require_once("connect.php");
//recuperar dados
if(isset($_POST['logar'])){
    echo 'clciou</br>';
    echo   $usuario= trim(strip_tags($_POST['usuario']));
    echo   $senha= trim(strip_tags($_POST['senha']));
    $select="SELECT*FROM login WHERE usuario=:usuario AND senha=:senha";
    echo $contar = $selec-> rowCount();
    try{
        $result = $conexao ->prepare($select);
        $result -> bindParam(':usuario',$usuario,PDO::PARAM_STR);
        $result -> bindParam(':senha',$senha,PDO::PARAM_STR);
        $result -> execute();
    }
    catch(PDOException $e){
        echo $e;
    }
}
?>

Form that it takes

<form action="#" method="post" enctype="multipart/form-data">
  <h1>Faça seu Login</h1>
  <div class="login-fields">
    <p>Entre com seus dados:</p>
    <div class="field">
      <label for="username">Usuário:</label>
      <input type="text" id="username" name="usuario" value="" placeholder="Usuário" class="login username-field" />
    </div>
    <!-- /field -->
    <div class="field">
      <label for="password">Senha:</label>
      <input type="password" id="password" name="senha" value="" placeholder="Senha" class="login password-field" />
    </div>
    <!-- /password -->
  </div>
  <!-- /login-fields -->
  <div class="login-actions">
    <input type="submit" name="logar" value="entrar no sistema" class="button btn btn-success btn-large" />
  </div>
  <!-- .actions -->
</form>

Could someone tell me what I do wrong?

    
asked by anonymous 21.02.2017 / 19:28

1 answer

0

Hello, boy look at the solution:

<?php
require_once("connect.php");

//recuperar dados
if(isset($_POST['logar']) == true){
echo 'clicou</br>';
//Melhor seria usar Expressões regulares aqui, é mais seguro        
//seria bom também passar o htmlentities($var, ENT_QUOTES | ENT_HTML5, 'UTF-8', false)
// nos campos e converter tudo para utf-8, o "false" diz à função que
// não tente converte se já estiver no formato utf-8
$usuario= trim(strip_tags($_POST['usuario']));
$senha= trim(strip_tags($_POST['senha']));

// Executar a instrução SQL     
$sql = "SELECT * FROM login WHERE nome = :usuario AND senha = :senha";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':usuario', $usuario,PDO::PARAM_STR);
$stmt->bindParam(':senha', $senha,PDO::PARAM_STR);

$stmt->execute();
// Se encontrar algumacoisa '$stmt->rowCount() > 0', continuar
if($stmt->rowCount() > 0 ){

//Percorrendo todos os registros encontrados
for($i=0; $row = $stmt->fetch(); $i++){
//Imprime o ID e nome do Usuário                
echo "O id &eacute;:".$row['id'].", o nome &eacute;:".$row['nome']."<br />";
}
} else {
//Caso não encontre nada escreva "Nadinha!" ou algo mais inteligente
echo "Nadinha!";
}
}
?>

This tray will not connect. I've been there like this and will not do it at all. Regarding this file "connect.php", it should look exactly like this:

<?php
//File connect.php
$dsn = 'mysql:host=localhost;dbname=nomedoseudatabase';
$user = 'usuariododatabase';
$password = 'senhadosusuariododatabase'; 
try {
//instaciar o objeto PDO    
$pdo = new PDO($dsn, $user, $password);
// adicionar suporte a utf-8
$pdo->exec("SET NAMES 'utf8' COLLATE 'utf8_general_ci'");
//dizer ao pdo o que fazer em caso de erro
} catch (PDOException $e) {echo 'Conexao sem sucesso: ' . $e->getMessage(); }
?>

I had to do this table in my Slackware db here to test, it works fine.

I think you should make it a habit to make a class for the database or classes. In my site I use several database, users, sessions and user profiles and preferences are in the same database. Already posts and other things that are placed on the site are in another and everything works round. He takes the posts and, running on each post, he checks the other database and takes everything about the user, including social networks if he has registered and places the schemas in the post for indexing in Google. If the user is logged in he automatically queries the profile table because the login class calls the profile class which checks if the person has a profile on the site and if it has all the preferences of the user in the session and this can be accessed by the whole site so that it no longer needs to put name in forms, as there are neither fields for name and emails if the person is logged in. Classes greatly simplifies things. Thank you! Good studies!

    
23.02.2017 / 00:01