Can’t I return all the input data with PDO?

Asked

Viewed 160 times

1

I created a page to put the cpf and soon after doing a database search, and brings the data according to the typed Cpf. but has some inputs that are not coming back as <radio> (sex M or F), the <select> are also not coming back, the <textarea> and the <input type="checkbox">.

here is the php code that does the search with the typed Cpf:

<?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 must return on each input according to 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 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 that I commented at the beginning still not enough. <radio> (sex M or F), the <select> are also not coming back, the <textarea> and the <input type="checkbox">.

1 answer

1

The inputs of the type checkbox, radio and select has the default value set differently from inputs of the kind text.

To checkbox and radio works the same way. You must add the attribute checked. That it might be him alone or checked="checked" (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 both to radio how much checkbox.

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

Since you use fixed values in HTML, it can be done in a similar way 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 attribute value, who possesses this attribute is the option that’s inside the select.

Last but not least, the textarea also does not have the attribute value. Unlike the inputs, which is a tag without closure, the textarea need an opening and closing tag. The content/value of textarea should be inserted between tags:

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

Other considerations

In all your code, you’re using print_r, print_r is not for output of the variable value, and yes to display the information, with an easy read (Human-readable), of a variable. In your case, you should use only print or echo.

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

The radio is under different names. The first is Sexo and the second sexo. He is case sensitive, should always keep the same case.

In the case of checkbox, he should be treated as array. In that case, all your inputs of the kind checkbox must have the name with the symbology of array (name="examesf[]").

So your checkbox would be:

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

There in PHP, you can iterate on the values selected:

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

Only for explanation. If the checkbox have a set value (value), only selected values will be sent to PHP. If checkbox for of name unique and no value, PHP will receive the value (On/Off).

  • It helped a lot, but I’m having a hard time in the 'Lected' I’m not able to pass the values of the 'option', I wanted to know if this is the only way, or if I’m doing wrong then. Something else in the checkbox I’m having difficulty but this should be because I’m not saved right in the bank, because the check has 5 or 3 options and the bank only saves 1 even if checking all the options together, only saves 1 option. no no html with checkbox te so so 'name=exams' 'value=blood', 'value=urine', 'value=no exam',

  • @Brunod.Ssis What is the problem with the select? Update your question with difficulty. On the question of checkbox is because your modeling is wrong. You need to have a second table to save data from checkbox and relate to the record you save. From a look in that article. Anyway, this last question would be interesting to be another question (because, the focus of this one is another).

Browser other questions tagged

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