How to pass data from database to combobox?

Asked

Viewed 108 times

1

I wanted to put some data from the database in a combobox but I’m not getting it, I think the problem will be the connection to the database but I don’t know what will be wrong, can help me?

<?php
...
$dbconn = mysqli_connect($servername, $username, $password, $dbname)or die("Failed to connect to database:" . mysqli_error($dbconn));

$query = "SELECT nome FROM Medicos";
$data = mysqli_query($dbconn, $query);
$result = mysqli_num_rows($data);       

?>
  <form name="medicos" method="post" action="">
  <label for="">Selecione um m&eacute;dico</label>
  <select>
  <option>Selecione...</option>

<?php while($prod = $result->fetch_assoc()) { 
   echo '<option value="'.$prod['nome'].'">'.$prod['nome'].'</option>';
   }
?>    
</select>
  • What’s the problem? Does it give an error? If so, edit the question and add the message. If not, is the result different than expected? If yes, describe what was the result obtained and what was expected.

1 answer

1


Do your while on variable $data because it is she who made the Query execution

<?php

...
$dbconn = mysqli_connect($servername, $username, $password, $dbname)or die("Failed to connect to database:" . mysqli_error($dbconn));

$query = "SELECT nome FROM Medicos";
$data = mysqli_query($dbconn, $query);
$result = mysqli_num_rows($data);       

?>
  <form name="medicos" method="post" action="">
  <label for="">Selecione um m&eacute;dico</label>
  <select>
  <option>Selecione...</option>

<?php while($prod = $data->fetch_assoc()) { 
   echo '<option value="'.$prod['nome'].'">'.$prod['nome'].'</option>';
   }
?>    
</select>
  • if instead of being for a combobox is for a textbox, just replace the whole part of select with an input type text?

  • that and in the name put the brackets to symbolize an array, example: '<input type="text" value="'. $valor_db. '" name="name[]"/>'

Browser other questions tagged

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