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

Asked

Viewed 2,832 times

-1

my system has to do a search, using a patient’s Cpf, then when he type his Cpf, have to have a search in the bank, where php will get all the information that the patient has with that Cpf that he typed (that is, he already has a register, with name, Cpf, age, gender and etc...) and bring everything in the inputs corresponding to the html page, the same page that he used to register, but will be a kind of clone of her.

the code of consultation

        <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’m having doubts about this code, it’s probably wrong and some lines are missing. wanted someone to explain me how to make a correct query structure in PDO.

2 answers

0

Add an isset to validate Cpf, add a ":Cpf" instead of the Cpf variable in the query, bind is to secure the query before executing, then run and take the result in $result, then in print_r look at the name that comes in the index of the array, will be the column name for example the patient name column be name in $result if you do echo $result['name'] is to come the patient name, if it doesn’t give so Tenten $result->name, then just play in the form, in the example I played the patient’s Cpf in the form=Cpf; ? >" if it doesn’t work like this try any doubt 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>
  • Thanks for your attention, I just can’t understand how I’m going to play this in the inputs. may seem like a stupid question and because I’m not able to understand it very well. but I did what you passed me and I saw that it returned only Cpf.

  • I made one more attempt here and now giving the error: Notice: Trying to get Property of non-object in C: wamp64 www test query.php on line 40 Call Stack #Timememoryfunctionlocation 10.0012243384{main}( )... query.php:0 "

  • in the input that should appear the information, ta appearing that <br /><font size='1'><table class='Xdebug-error Xe-notice' dir='Ltr' border='1' cellspacing='0' cellpadding='1'><tr><th align='left' bgcolor='#f57900' colspan=

  • do so @Brunod.Assis there at print_r($result) show the exit please

  • Array ( [0] => Array ( [Cpf] => 111.111.111-11 [0] => 111.111.111-11 ) ) and this is what print_r($result) returns.

  • So @Brunod.Ssis for example if you want to show his Cpf in the Cpf field just do it look like this <div> <input class="field-form" id="Cpf" type="text" name="Cpf" placeholder="CPF" maxlength="14" value="<? php echo $result[0]['Cpf'];? >"><br> </div> If you want to show name and email and such you need to change your query, because it only filters Cpf you can do the query "SELECT * FROM patient Where Cpf = $Cpf"

  • got it, in the case here I have are 39 fields. I would have to do <?php echo $result[0]['field'];? > in each value and that? example: <?php echo $result[1]['name'];? >, <?php echo $result[2]['email'];? > and etc?

  • Yes, it would have @Brunod.

Show 3 more comments

0

Take this example, it can be useful, done in jquery, if you just want to comment, and I get your questions.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="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 properly requested.

<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;
  • I suggest you give your read on this topic, it costs nothing to research! https://answall.com/questions/111789/comort_datas-espec%C3%Adficos-de-uma-tabela-com-Pdo

Browser other questions tagged

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