Query in MySQL with AJAX

0

I created a "usuario" table in MySQL containing name and password. I established a connection with PHP and would like to query for AJAX , to validate this name and password and print a message saying whether they are valid or not. But I do not know how to do the query part in the inputs.

<html>
<head>
    <title>Exercício Tec Prog I</title>
    <meta charset="UTF-8">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><script>$(document).ready(function(){$("#bEntrar").click(function(){
                fLogar();


            });

            function fLogar()
            {
                $.ajax({
                    type: 'POST',
                    dataType: "json",
                    url: "usuario.php",
                    success:function(dados)
                    {
            }   

        });         
    </script>
</head>
<body>

    <form action="usuario.php" method="POST">
    <label>Usuario</label>
    <input type="text" id="tLogin"/>
    <label>Senha</label>
    <input type="text" id="tSenha">
    <button id="bEntrar">Entrar</button>
    <div id="divFilmes"></div>
</form>

</body>
</html>


     <?php



 $tLogin = $_POST["tLogin"];
 $tSenha = $_POST["tSenha"];

    //Conectando ao banco de dados
 $con = new mysqli("localhost", "root", "root", "tecprog");
 if (mysqli_connect_errno()) trigger_error(mysqli_connect_error());

    //Consultando banco de dados
 $qryLista = mysqli_query($con,"SELECT * FROM usuario WHERE nome = '$tLogin' AND senha = '$tSenha'");    
 while($resultado = mysqli_fetch_assoc($qryLista)){
    $vetor[] = array_map('utf8_encode', $resultado); 
 }    

    //Passando vetor em forma de json
 echo json_encode($vetor);

 ?>
    
asked by anonymous 03.04.2018 / 15:34

1 answer

0

If it's just to check if the user exists, just do this:

  $tLogin = $_POST["tLogin"];
  $tSenha = $_POST["tSenha"];

//Conectando ao banco de dados
$con = new mysqli("localhost", "root", "root", "tecprog");
if (mysqli_connect_errno()) trigger_error(mysqli_connect_error());

 if($con->query("SELECT * FROM usuario WHERE nome ='$tLogin' AND senha = '$tSenha'")->fetch_all()){
    //Usuário existe
    echo 'Existe';
 }else{
    //Usuário não existe
     echo 'Não existe';
 }

Your inputs were without the name field, so try:

<form action="usuario.php" method="POST">
<label>Usuario</label>

<input type="text" id="tLogin" name="tLogin"/>

<label>Senha</label>

<input type="text" id="tSenha" name="tSenha">

<button id="bEntrar">Entrar</button>

<div id="divFilmes"></div>
    
03.04.2018 / 16:06