Redeem name and show in option through id

1

I have a id of the table, example:

user with id = 1 , how can I get the name of this user using id of it and show on input ?

I did so:

while ($dados = mysql_fetch_array ($sql)) {?>
    <option value="<?php echo $dados["id"] ?>" ><?php echo $dados["nome"]; ?></option>

When he selects the option to show in a input the description of what it would be, for example:

  • id = 1 : banana (show banana no input)
  • id = 2 : meat (show meat not input)

I know it's an esdrúxulo example because I do not know how to explain

    
asked by anonymous 03.10.2016 / 13:49

4 answers

1
<?php
$id = 1;
$sql = "SELECT nome FROM table WHERE id= '.$id.'";
$result = mysql_query($sql, $connection) or die (mysql_error());
$row = mysql_fetch_array($result); ?>


<select name="nome">
  <?php while($row = mysql_fetch_array($result)){ ?>
     <option value="<?=$row['id']?>"><?=$row['nome'] ?></option>
  <?php } ?>
</select>
    
03.10.2016 / 15:31
0

Search the Bank by selecting all the fields with the id or just the fields you need;

"SELECT * FROM sua_tabela WHERE id = 'id'" 

And in the input you put the data you want.

    
03.10.2016 / 14:00
0

As @LeoLetto said: Do a search on the Bank by selecting all the fields with the id or just the fields that you need:

SELECT * FROM sua_tabela WHERE id = 'id'

And in the input you put the data you want.

Example with your code:

$id = '1';
$sql = SELECT * FROM nome_da_sua_tabela WHERE id = 'id'; // usando * você poderá usar todos os campos (nome, id, etc), ou você pode usar SELECT nome, assim você vai selecionar somente o campo nome da tabela que tem o id = 1
$resultado = mysqli_query($con, $sql); // $con é a variavel que faz a conexão com o banco. 
while($dados = mysqli_fetch_array($resultado))
{ // Lembre de fechar com ?> e no final do seu while você irá abrir o php e fechar o while.


<option value="<?php echo $dados["id"] ?>" ><?php echo $dados["nome"]; ?></option>
    
03.10.2016 / 15:37
0

Assuming your <select> and query sql are in the same file and that you are using the mysql plugin of php5 as quoted in the question:

<?php
/*Conexão com base de dados e variável de id sendo preenchida aqui
.
.
*/
$sql = "SELECT id, nome FROM tb_pessoa WHERE id= '$id'";
$select = mysql_query($sql, $connection) or die (mysql_error());
$array = mysql_fetch_assoc($select); ?>


<select name="nome">
  <?php 
    foreach($array as $row){
      echo "<option value='".$row['id']."'>".$row['nome']."</option>"
  ?>
</select>
    
11.10.2018 / 14:29