Getting data from MySQL database into html drop-down list

I have a website containing an html form, in this form I have a drop-down list with a list of agents that works in the company, I want to get data from the MySQL database into this drop-down list, so when you add a new agent, the name will appear as an option in the drop-down list.

Can you help me in coding this PHP code please thanks

<select name="agent" id="agent"> </select> 
+6
source share
3 answers

To do this, you want to view each row of query results and use this information for each of the drop-down options. You should be able to adjust the code below easily enough to meet your needs.

 // Assume $db is a PDO object $query = $db->query("YOUR QUERY HERE"); // Run your query echo '<select name="DROP DOWN NAME">'; // Open your drop down box // Loop through the query results, outputing the options one by one while ($row = $query->fetch(PDO::FETCH_ASSOC)) { echo '<option value="'.$row['something'].'">'.$row['something'].'</option>'; } echo '</select>';// Close your drop down box 
+11
source
 # here database details mysql_connect('hostname', 'username', 'password'); mysql_select_db('database-name'); $sql = "SELECT username FROM userregistraton"; $result = mysql_query($sql); echo "<select name='username'>"; while ($row = mysql_fetch_array($result)) { echo "<option value='" . $row['username'] ."'>" . $row['username'] ."</option>"; } echo "</select>"; # here username is the column of my table(userregistration) # it works perfectly 
+14
source

What you ask is pretty straightforward

  • query your db to get a result set or use the API to get a result set

  • scroll through the list of results or just the result using php

  • In each iteration, just format the output as an element

the following link should help

HTML tag

Retrieving data from a MySQL database

hope this helps :)

0
source

Source: https://habr.com/ru/post/912377/


All Articles