how to do a search in mysql with php and return the data in each input?

-1

My system has to do a search, using the cpf of a patient, then when he enters his cpf, he has to have a search in the bank, where the php will get all the information that the patient has with that cpf that he (ie he already has a registration, with name, cpf, age, sex and etc ...) and bring everything in the corresponding inputs of the html page, the same page that he used to register, but will be type one clone of it.

the query code                  

        <h2>Consultar Ficha do Paciente</h2>

        <div>
            <input class="campo-form" type="text" name="cpf" placeholder="CPF" maxlength="14"></br>
        </div>

        <div class="groupb">
            <button class="botao" type="submit">Buscar</button>
        </div>

        <div class="groupb">
            <a href="php/menuNutricionista.php" class="botoes">Voltar</a>
        </div>

    </form>

the php code

<?php include("conexao.php"); $pdo=conectar(); $cpf = $_POST['cpf']; $consulta= $pdo->prepare("SELECT cpf FROM paciente where cpf = $cpf"); ?>

I am in doubt about this code, probably wrong and with some missing lines. I wanted someone to explain to me how to do a correct PDO query structure.

    
asked by anonymous 09.11.2017 / 05:33

2 answers

0

Add an isset to validate cpf, add a ": cpf" instead of the cpf variable in the query, bind is to query the query before executing, then executes and takes the result in $ result, then in print_r look at the name that comes in the index of the array, it will be the name of the column for example the column of the name of the patient be name in $ result if you do echo $ result ['name'] is to see the patient's name, if do not give so try $ result-> name, then just play on form, in the example I played the patient's cpf in form value = cpf; ? > "if you do not give it try any doubts shout there in the comment

<?php 
include("conexao.php"); 
$pdo=conectar(); 
$cpf = $_POST['cpf'];
if(isset($cpf)){
$consulta= $pdo->prepare("SELECT cpf FROM paciente where cpf = :cpf");
$consulta->bindParam(":cpf",$cpf);
$consulta->execute();
$resultado = $consulta->fetchAll();
print_r($resultado);

}
?>

<h2>Consultar Ficha do Paciente</h2>

        <div>
          <input class="campo-form" type="text" value="<?php echo $resultado->cpf; ?>" name="cpf" placeholder="CPF" maxlength="14"></br>
        </div>

        <div class="groupb">
            <button class="botao" type="submit">Buscar</button>
        </div>

        <div class="groupb">
            <a href="php/menuNutricionista.php" class="botoes">Voltar</a>
        </div>

    </form>
    
09.11.2017 / 11:45
0

See this example, it can be useful, done in jquery, if you just want to comment, and shoot your doubts.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.12/jquery.mask.min.js" type="text/javascript"></script>
<script type="text/javascript">
   $(function () {
      $(".cpf").mask("999.999.999-99");

      $(document).on("click", ".botao", function () {
        var cpf = $('.cpf').val();
           $.ajax({
              url: 'exemplo.php',
              type: 'POST',
              data: 'acao=search&cpf=' + cpf,
              dataType: 'json',
              beforeSend: function () {
              },
              success: function (data) {
                 $("#cpf").val(data[0].user_cpf);
                 $("#nome").val(data[0].user_name);
                 $("#email").val(data[0].user_email);
               },
               error: function (data) {
                  alert('Erro ao solicitar dados');
               }
           });
        return false;
      });
    });

This is the form you use to send the data

<form action="" method="post" id="form">
        <h2>Consultar Ficha do Paciente</h2>

        <div>
            <input class="campo-form cpf" type="text" name="cpf" placeholder="CPF" maxlength="14"><br>
        </div>

        <div class="groupb">
            <button class="botao" type="button">Buscar</button>
        </div>

        <div class="groupb">
            <a href="php/menuNutricionista.php" class="botoes">Voltar</a>
        </div>

    </form>

And this is the form that receives the data itself.

<form action="" method="post">
        <h2>Consultar Ficha do Paciente</h2>

        <div>
            <input class="campo-form" id="cpf" type="text" name="cpf" placeholder="CPF" maxlength="14"><br>
        </div>
        <div>
            <input class="campo-form" id="nome" type="text" name="nome" placeholder="Nome"><br>
        </div>
        <div>
            <input class="campo-form" id="email" type="text" name="email" placeholder="E-mail"><br>
        </div>


        <div class="groupb">
            <a href="php/menuNutricionista.php" class="botoes">Voltar</a>
        </div>

    </form>

And here is my php file that does the search.

header('Content-Type: application/json');
$acao = filter_input(INPUT_POST, 'acao', FILTER_DEFAULT);
switch ($acao):
case 'search':
    $array = array();
    $read = new Read;
    $read->ExeRead("ws_users", "where user_cpf = :cpf", "cpf={$_POST['cpf']}");

    if ($read->getResult()):
        $array[] = $read->getResult()[0];
        echo json_encode($array);
    else:
        echo json_encode(array('erro' => 'Erro ao consultar'));
    endif;
    break;
endswitch;
    
09.11.2017 / 13:25