Fetching Data from MySQL Database to HTML Dropdown List

Fetching data from MySQL database to html drop-down list

In MySQLi, the first parameter of a query needs to be the database connection. Also, there's no need to add a \ before the statement.

$sql = \mysqli_query("SELECT name From users"); should be $sql = mysqli_query($con, "SELECT name From users");

Note: replace $con with your database connection variable!

As you mentioned that you wanted the result from the database to go inside the select form, simply adjust your code to look like this:

<select name="to_user" class="form-control">
<option value="pick">בחר מהרשימה</option>
<?php
$sql = mysqli_query($con, "SELECT name From users");
$row = mysqli_num_rows($sql);
while ($row = mysqli_fetch_array($sql)){
echo "<option value='". $row['name'] ."'>" .$row['name'] ."</option>" ;
}
?>
</select>

Populate an HTML DropDown List with data from mysql database without PHP

The easy answer

plain html

<select name="clients">
<option value="1">Thomas Carrier</option>
<option value="2">Michel Carrier</option>
...
</select>

or with ruby (https://guides.rubyonrails.org/layouts_and_rendering.html)

<select name="clients">
<% @clients.each do |client| %>
<option value="<%= client.id %>"><%= client.firstName %> <%= client.lastName %></option>
<% end %>
</select>

Also refer to Yaniv Iny's answer to for a better more correct way

Problem when Fetching data from MySQL database to html drop-down list

Remove <select name="select1"> 
and close option tag properly </option>

Fetch data from mysql database based on multiple dropdown selections

You can check if the options are set by using
isset() and if the value is not set, Just assign '' a string of length zero to those variables.

if(isset($_POST['get_option1'])){
$state1 = $_POST['get_option1'];
}else{
$state1 = '';
}

Similarly for all other variables.

Fetching data from MySQL database to HTML dropdown list

To do this you want to loop through each row of your query results and use this info for each of your drop down's options. You should be able to adjust the code below fairly easily 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="'.htmlspecialchars($row['something']).'">'.htmlspecialchars($row['something']).'</option>';
}

echo '</select>';// Close your drop down box

Fetch data from mysql database using drop down list and then use search function to filter data from selected drop down list option

If I've understood correctly then you want to only search the column that the drop down has selected. Then you want to use this:

if (isset($_POST['search'])) {

$search_term = mysql_real_escape_string($_POST['search_box']);

switch($_POST['filter']) {

Default:
$sql .= " WHERE F_ar = '$search_term' ";
break;
case 1:
$sql .= " WHERE Postnr = '$search_term' ";
break;
case 2:
$sql .= " WHERE Postort = '$search_term' ";
break;
case 3:
$sql .= " WHERE Vardgivare = '$search_term' ";
break;
case 4:
$sql .= " WHERE Team = '$search_term' ";
break;
case 5:
$sql .= " WHERE Orsak = '$search_term' ";
break;
case 6:
$sql .= " WHERE Planerat_datum = '$search_term' ";
break;
case 7:
$sql .= " WHERE fran = '$search_term' ";
break;
case 8:
$sql .= " WHERE AAA_diam = '$search_term'; ";
break;

}
}


As for your error, you've got two forms, you need everything to be in one form like this:

<form name="Select_filter" method="POST" action="VGR_data_display.php">
<select id="dropdown" name="filter">
<option value=""></option>
<option value="1">ID</option>
<option value="2">Alder</option>
<option value="3">Postnummer</option>
<option value="5">Postort</option>
<option value="6">Vårdgivare</option>
<option value="7">Planerat Datum</option>
<option value="8">Status</option>
<option value="9">AAA_diameter</option>
</select>


<!--search bar for search term input -->

<input id="search_box" type="text" name="search_box" value="" />
<input id="submit" type ="submit" name ="search" value ="Ok">

</form>


Related Topics



Leave a reply



Submit