Redeem name and show in option through id

Asked

Viewed 1,661 times

1

I have a id table, example:

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

I did so:

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

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

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

I know it’s a weird example why I don’t know how to explain

  • To display data, you can use the following form: while($dados = mysql_fetch_array($sql)){&#xA; echo "<input type='text' value='" . $dados['id']. " - ". $dados['nome'] + "' />"; }

  • Sorry I forgot a detail the input I want to pull the name is out of the while

  • You can create a variable outside the loop and set its value for display. Ex: `$value = '; while(.....) { $value = $data['name']; ... } echo $value;

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>

0

Do a search in the Database by selecting all fields with the id or only the fields you need;

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

And in the input you put the data you want.

0

As @Leoletto said: Do a Database search by selecting all 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.

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>
  • while ($dados = mysql_fetch_array ($sql)) {&#xA; echo "<option value=". $dados['id'] ."> ". $dados['nome'] . "</option>";&#xA; } To show select worked perfectly but I want to show in an input another example field: <input type="text" name="desc" value="<? php $data['desc'] ? >" readonly/> but with this I need to make a query using data['id'] only then I get lost

  • @Vagner You can print html directly in PHP, or use it the way I mentioned it.

0

Supposing that your <select> and consultation sql are in the same file and that you are using the plugin mysql 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>

Browser other questions tagged

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