I can not return all the data in the inputs with the PDO?

1

I created a page to put cpf and soon after it does a search in the bank, and it brings the data according to the cpf typed. but there are some inputs that are not returning as <radio> (sex M or F), <select> are also not returning, <textarea> and <input type="checkbox"> .

Here is the php code that does the search with the cpf typed:

<?php  include("php/conexao.php"); $pdo=conectar(); 
       $cpf = $_POST['cpf']; 
       if(isset($cpf)){ 
         $consulta= $pdo->prepare("SELECT * FROM paciente where cpf = :cpf"); 
         $consulta->bindParam(":cpf",$cpf);
         $consulta->execute(); $resultado = $consulta->fetchAll(); print_r($resultado); 
       } 
       if(!$resultado){ 
        echo "<script>alert('Dados informados incorretamente!');history.back();</script>"; 
       } 
?>

Here and where the data should return in each input as per the database tb:

<!DOCTYPE html>
<html lang="en-US">

    <head>
        <meta charset="UTF-8">
        <title>NutricLIFE</title>
        <link rel="stylesheet" type="text/css" href="css/style.css">
        <link href="https://fonts.googleapis.com/css?family=Merriweather" rel="stylesheet">
        <link rel="shortcut icon" href="imagens/favicon.png" />
    </head>

    <body id="body1">

        <img id="emblema" src="imagens/emblema.png">

        <div id="body2">

            <div class="groupb1">
                <a href="index.html" class="botoes1">PDF</a>
            </div>

      <div class="groupb1">
        <a href="consultarPaciente.html" class="botoes1">voltar</a>
      </div>

    </div>

            <div class="box-paciente">

                <form action="#.php" method="get">

                    <h2>Dados do Paciente</h2>

                      <div>
                          <input class="campo-form-c" type="text" name="nome" placeholder="Nome" value=<?php print_r($resultado[0]['nome']); ?>>
                    </div>

                    <div>
                          <input class="campo-form-c" type="text" name="cpf" placeholder="CPF" value=<?php print_r($resultado[0]['cpf']); ?> >
                    </div>


                    <div>
                            <input class="campo-form-c" type="text" name="ultimopeso" placeholder="Ultimo peso" value=<?php print_r($resultado[0]['ultimopeso']); ?>></br>
                    </div>

                    <div>
                          <input class="campo-form-c" type="text" name="pesoatual" placeholder="Peso Atual" value=<?php print_r($resultado[0]['pesoatual']); ?>>
                      </div>

                    <div>
                        <input class="campo-form-c" type="text" name="altura" placeholder="Altura" value=<?php print_r($resultado[0]['altura']); ?>>
                    </div>

                    <div>
                        <input class="campo-form-c" type="number" name="idade" placeholder="Idade" value=<?php print_r($resultado[0]['idade']); ?>></br>
                    </div>

                    <div>
                        <input class="campo-form-c" type="text" name="datan" placeholder="Data de nascimento" value=<?php print_r($resultado[0]['datan']); ?>></br>
                    </div>

                    <div>
                        <div class="label-s">
                            <label>Sexo: </label>  
                                <label class="radio-inline">
                                    <input type="radio" name="sexo" value=<?php print_r($resultado[0]['sexo']); ?>>Masculino
                                </label>

                                <label class="radio-inline">
                                    <input type="radio" name="Sexo" value=<?php print_r($resultado[0]['sexo']); ?>>Feminino
                                </label>
                        </div>
                    </div>

                 <div>
            <input class="campo-form-c" type="text" name="medidab" placeholder="Medida do Braço" maxlength="5" value=<?php print_r($resultado[0]['medidab']); ?>>
          </div>

          <div>
            <input class="campo-form-c" type="text" name="medidat" placeholder="Medida Tricipital" maxlength="5" value=<?php print_r($resultado[0]['medidat']); ?>>
          </div>

            <div>
            <input class="campo-form-c" type="text" name="medidaabd" placeholder="Medida Abdominal" maxlength="5" value=<?php print_r($resultado[0]['medidaabd']); ?>></br>
            </div>

          <div>
            <input class="campo-form-c" type="text" name="imc" placeholder="IMC" maxlength="4" value=<?php print_r($resultado[0]['imc']); ?>>
          </div>

          <div>
            <input class="campo-form-c" type="text" name="gorduratricpital" placeholder="Gordura Tricipital" maxlength="5" value=<?php print_r($resultado[0]['gorduratricpital']); ?>>
          </div>

            <div>
            <input class="campo-form-c" type="text" name="circunferenciabraco" placeholder="Circunferência do Braço" maxlength="5" value=<?php print_r($resultado[0]['circunferenciabraco']); ?>></br>
            </div>


            <div>
            <input class="campo-form-c" type="text" name="circunferenciambraco" placeholder="Circunferência Muscular do Braço" maxlength="5" value=<?php print_r($resultado[0]['circunferenciambraco']); ?>></br>
            </div>

            <div>
            <input class="campo-form-c" type="text" name="gorduracorporal" placeholder="Gordura Corporal" maxlength="5" value=<?php print_r($resultado[0]['gorduracorporal']); ?>>
          </div>

            <div class="div-label">
              <label>Alguma alergia a medicamentos?</label> 
                <select class="select-form" name="alergiamedicamento" value=<?php print_r($resultado[0]['alergiamedicamento']); ?>>
                  <option>Sim</option>
                  <option>Não</option>
              </select>
              </div>

                    <div class="div-label">
                    <label>Utiliza algum medicamento?</label> 
                        <select class="select-form" name="algummedicamento" value=<?php print_r($resultado[0]['algummedicamento']); ?>>
                            <option>Sim</option>
                            <option>Não</option>
                    </select>
                    </div>

                    <div>
                    <label class="div-label">Quais?</label>
                        <textarea class="areat" name="quaismedicamentos" placeholder="Rascunho" value=<?php print_r($resultado[0]['quaismedicamentos']); ?>></textarea>
                    </div>

                    <div>
                        <input class="campo-form-c" type="text" name="tempomedicamento" placeholder="Tempo de medicamento" value=<?php print_r($resultado[0]['tempomedicamento']); ?>>
                    </div>

                    <div>
                    <label class="div-label">Bebe Água?</label> 
                        <select class="select-form" name="bebeagua" id="inputindex" value=<?php print_r($resultado[0]['bebeagua']); ?>>
                            <option>Sim</option>
                            <option>Não</option>
                        </select>
                    </div>

                       <div>
              <label class="div-label">Quantos copos?</label> 
                            <input class="select-form" type="number" name="quantoscopos" placeholder="Quantos Copos" value=<?php print_r($resultado[0]['quantoscopos']); ?>></br>
                   </div>

                    <div>
                    <label class="div-label">Frequência intestinal ?</label>
                        <select class="select-form" name="intestinal" value=<?php print_r($resultado[0]['intestinal']); ?>>
                            <option>Todos dos dias</option>
                            <option>Há cada dois dias</option>
                            <option>Há cada três dias</option>
                        </select>
                    </div>

                    <div>
                    <label class="div-label">Urina com Frequência ?</label> 
                        <select class="select-form" name="urina" value=<?php print_r($resultado[0]['urina']); ?>>
                            <option>Sim, todos os dias</option>
                            <option>Poucas vezes por dia</option>
                            <option>Quase não urino durante o dia</option>
                        </select>
                    </div>

                    <div>
                    <label class="div-label">Fuma ?</label> 
                        <select class="select-form" name="fuma" value=<?php print_r($resultado[0]['fuma']); ?>>
                            <option>Sim</option>
                            <option>Não</option>
                        </select>
                    </div>

                    <div>   
                    <label class="div-label">Ingere Bebida alcoólica ?</label>
                        <select class="select-form"" name="alcool" value=<?php print_r($resultado[0]['alcool']); ?>>
                            <option>Sim</option>
                            <option>Não</option>
                        </select>
                    </div>

                    <div>
                    <label class="div-label">Pratica alguma atividade física ?</label>
                        <select class="select-form" name="atividadefisica" value=<?php print_r($resultado[0]['atividadefisica']); ?>>
                            <option>Sim</option>
                            <option>Não</option>
                        </select>
                    </div>

                    <div>
                        <input class="campo-form-c" type="text" name="pratica" placeholder="Qual atividade física ?" value=<?php print_r($resultado[0]['pratica']); ?>></br>
                    </div>

                    <div>
                    <label class="div-label">Quantas vezes por semana ?</label> <!-- quantas vezes por semana -->
                        <select class="select-form" name="vezes" value=<?php print_r($resultado[0]['vezes']); ?>>
                            <option>0</option>
                            <option>1</option>
                            <option>2</option>
                            <option>3</option>
                            <option>4</option>
                            <option>5</option>
                            <option>6</option>
                            <option>7</option>
                        </select>
                    </div>

                    <div>
                    <label class="div-label">Intenção da Consulta ?</label> <!-- intenção da consulta -->
                        <textarea class="areat" name="consulta" placeholder="Rascunho" value=<?php print_r($resultado[0]['consulta']); ?>></textarea>
                    </div>

                    <div>
                    <label class="div-label">Problemas de Saúde ?</label> <!-- problema de saúde -->
                        <select class="select-form" name="saude" value=<?php print_r($resultado[0]['saude']); ?>>
                            <option>Sim</option>
                            <option>Não</option>
                        </select>
                    </div>

                    <div>
                    <label class="div-label">Histórico familiar ?</label> <!-- histórico familiar -->
                        <textarea class="areat" name="familiar" placeholder="Rascunho" value=<?php print_r($resultado[0]['familiar']); ?>></textarea>
                    </div>

                    <div>
                    <label class="div-label">Possui alergia a algum alimento ?</label> 
                         <textarea class="areat" name="comment" name="alergia" placeholder="Rascunho" value=<?php print_r($resultado[0]['alergia']); ?>></textarea>
                    </div>

                    <div>
                    <label class="div-label">Anamnese Alimentar: O que o paciente costuma comer de segunda a sexta ?</label> <!-- o que costuma comer de segunda a sexta-->
                        <textarea class="areat" name="anamnese" placeholder="Rascunho" value=<?php print_r($resultado[0]['anamnese']); ?>></textarea>
                    </div>

                    <div>
                    <label class="div-label">E sábado e domingo ?</label> <!-- o que costuma comer sabado e domingo-->
                        <textarea class="areat" name="sabadoedomingo" placeholder="Rascunho" value=<?php print_r($resultado[0]['sabadoedomingo']); ?>></textarea>
                    </div>

                    <div>
                        <div>
                            <label class="div-label">Tem exames recentes de Sangue? Urina ? Fezes? (Menos de 3 meses)</label>
                        </div>

                    <div class="checkb">
                            <label class="checkbox-inline">
                                <input type="checkbox" name="examesf" value=<?php print_r($resultado[0]['examesf']); ?> >Sangue |
                            </label>
                            <label class="checkbox-inline">
                                <input type="checkbox" name="examesf" value=<?php print_r($resultado[0]['examesf']); ?>>Urina |
                            </label>
                            <label class="checkbox-inline">
                                <input type="checkbox" name="examesf" value=<?php print_r($resultado[0]['examesf']); ?>>Fezes |
                            </label>
                            <label class="checkbox-inline">
                                <input type="checkbox" name="examesf" value=<?php print_r($resultado[0]['examesf']); ?>>Sem examesf
                            </label>
                        </div>

                            <textarea class="areat" name="examesfr" placeholder="Rascunho" value=<?php print_r($resultado[0]['examesfr']); ?>></textarea>
                    </div>

                    <div>
                        <div>
                            <label class="div-label">Solicitar exames :</label>
                        </div>
                        <div class="checkb">
                            <label class="checkbox-inline">
                                <input type="checkbox" name="examess" value=<?php print_r($resultado[0]['examess']); ?>>Sangue |
                            </label>
                            <label class="checkbox-inline">
                                <input type="checkbox" name="examess" value=<?php print_r($resultado[0]['examess']); ?>>Urina |
                            </label>
                            <label class="checkbox-inline">
                                <input type="checkbox" name="examess" value=<?php print_r($resultado[0]['examess']); ?>>Fezes 
                            </label>
                        </div>

                            <textarea class="areat" name="examessr" placeholder="Rascunho" value=<?php print_r($resultado[0]['examessr']); ?>></textarea>
                    </div>
                </form>
            </div>
    </body>
</html>

using print_r in the php code it returns the array:

  

Array ( [0] => Array ( [id_paciente] => 1004 [0] => 1004 [nome] => fulano [1] => fulano [cpf] => 111.111.111-11 [2] => 111.111.111-11 [ultimopeso] => 90 [3] => 90 [pesoatual] => 75 [4] => 75 [altura] => 1.93 [5] => 1.93 [idade] => 20 [6] => 20 [datan] => 11-11-2000 [7] => 11-11-2000 [sexo] => M [8] => M [medidab] => 12.34 [9] => 12.34 [medidat] => 12.54 [10] => 12.54 [medidaabd] => 32.54 [11] => 32.54 [imc] => 12.3 [12] => 12.3 [gorduratricpital] => 50.32 [13] => 50.32 [circunferenciabraco] => 12 [14] => 12 [circunferenciambraco] => 12 [15] => 12 [gorduracorporal] => 12 [16] => 12 [alergiamedicamento] => Sim [17] => Sim [algummedicamento] => Sim [18] => Sim [quaismedicamentos] => exemplo. [19] => exemplo. [tempomedicamento] => exemplo. [20] => exemplo. [bebeagua] => Sim [21] => Sim [quantoscopos] => 10 [22] => 10 [intestinal] => Todos dos dias [23] => Todos dos dias [urina] => Quase n�o urino durante o dia [24] => Quase n�o urino durante o dia [fuma] => Sim [25] => Sim [alcool] => Sim [26] => Sim [atividadefisica] => Sim [27] => Sim [pratica] => exemplo. [28] => exemplo. [vezes] => 7 [29] => 7 [consulta] => exemplo. [30] => exemplo. [saude] => Sim [31] => Sim [familiar] => exemplo. [32] => exemplo. [alergia] => exemplo. [33] => exemplo. [anamnese] => exemplo. [34] => exemplo. [sabadoedomingo] => exemplo. [35] => exemplo. [examesf] => Fezes [36] => Fezes [examesfr] => exemplo. [37] => exemplo. [examess] => Sangue [38] => Sangue [examessr] => exemplo. [39] => exemplo. ) )

But the input I commented at the beginning have not yet arrived. <radio> (sex M or F), <select> are also not returning, <textarea> and <input type="checkbox"> .

    
asked by anonymous 18.11.2017 / 01:58

1 answer

1

Inputs of type checkbox , radio and select have the default value set differently than inputs of type text .

For checkbox and radio works the same way. You must add the checked attribute. That can be it alone or checked="checked" (of xHTML):

<?php 

$isMale = $resultado[0]['sexo'] == "M" ? 'checked="checked"' : '';
$isFemale = $resultado[0]['sexo'] == "F" ? 'checked="checked"' : ''; 

?>

<input type="radio" name="sexo" value="M" <?= $isMale; ?> />Masculino
<input type="radio" name="Sexo" value="F" <?= $isFemale; ?> />Feminino

This provision works for both radio and checkbox .

As for select , you must define which option of select is selected. It is similar to checkbox , but instead of checked will be selected .

Since you use fixed values in HTML, it can be done similarly to checkbox .

<?php 

$isTrue = $resultado[0]['saude'] == "S" ? 'selected="selected"' : '';
$isFalse = $resultado[0]['saude'] == "N" ? 'selected="selected"' : ''; 

?>

<select class="select-form" name="saude">
    <option value="S" <?= $isTrue; ?> />Sim</option>
    <option value="N" <?= $isFalse; ?> />Não</option>
 </select>

Another point, select does not have the value attribute, who has this attribute is the option that is within select .

Last, but not least, textarea also does not have the value attribute. Unlike inputs , which is a non-closing tag, textarea needs opening and closing tags. The content / value of textarea must be inserted between tags:

<textarea class="areat" name="quaismedicamentos" placeholder="Rascunho"><?=  $resultado[0]['quaismedicamentos']; ?></textarea>

Other considerations

Throughout your code, you are using print_r , print_r is not for output of the variable value, but to display the information, human-readable ), of a variable. In your case, you should only use print or echo .

Your HTML is also having some problems, so there is no right return for saving to the database.

radio has different names. The first is Sexo and the second sexo . It is case sensitive, it should always keep the same case.

For checkbox , it should be treated as array . In this case, all your inputs of type checkbox should have name with the symbology of array ( name="examesf[]" ).

So your checkboxes would be:

<input type="checkbox" name="examesf[]"/>

In PHP, you can iterate over the selected values:

foreach($_POST['examesf'] as $value)
{
    echo $value;
}

For explanation only. If checkbox has a set value ( value ), only selected values will be sent to PHP. If checkbox is of name single and has no value, PHP will receive the value ( On / Off ).

    
18.11.2017 / 14:02