[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 343
  • Last Modified:

PHP Populating Combo Box with data from a table

hi
I have a combo box and need to populate it with data from a database.
How do i do that?
Cheers
mat
0
funkkube
Asked:
funkkube
2 Solutions
 
ludofulopCommented:
if you have for example table 'combovalues', that contains column 'combovalue', then this way:


<select name="comboname">
<?php
  $result = mysql_query('select * from combovalues');
  while ($row = mysql_fetch_assoc($result)) {
   echo '<option value="'.$row['combovalue'].'">'.$row['combovalue'].'</option>';
  }
?>
</select>

(not tested in real enviroment)
0
 
psantos_aoCommented:
first of all... make a connection to the database
<?php
 $link = mysql_connect("localhost","root","pass") or die (mysql_error());
$db = mysql_select_db("data_base") or die (mysql_error());

$query = "SELECT * FROM countries";
$sql  = mysql_query($query) or die (mysql_error());
?>
<!-- Building our form -->
<form action="" method="post">
Country List:
<select name="country">
<?php
while ($row = mysql_fetch_assoc($sql)){
  echo "<option value='".$row['id_country']."'>".$row['country_name']."</option>";
}
?>
</select>
<input type="submit" value="Send" />
</form>


0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now