Populate a Drop Down Box from a MySQL Table in PHP

Populate a Drop down box from a mySQL table in PHP

You will need to make sure that if you're using a test environment like WAMP set your username as root.
Here is an example which connects to a MySQL database, issues your query, and outputs <option> tags for a <select> box from each row in the table.

<?php

mysql_connect('hostname', 'username', 'password');
mysql_select_db('database-name');

$sql = "SELECT PcID FROM PC";
$result = mysql_query($sql);

echo "<select name='PcID'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['PcID'] . "'>" . $row['PcID'] . "</option>";
}
echo "</select>";

?>

Populating Php Dropdown list from mysql database

Depending on whats in your dbcon.php, but here's an example using mysqli_query:

<div class="form-group">
Select Make <?php
// start of dbcon
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

$conn = new mysqli($servername, $username, $password, $dbname);
//end of dbcon

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT * FROM vehicle_details";
$result = $conn->query($sql);

if ($result->num_rows > 0) {

echo "<select name='vehicle_make'>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<option value='" . $row['vehicle_make'] . "'>" . $row['vehicle_make'] . "</option>";
}
echo "</select>";
}
$conn->close();
?>
</div>

Populate dropdown from Another MySQL table - php

This is assuming you have an object $student which is the row from students corresponding to the current user.

$dropdown_query = "SELECT * FROM course";
$courses = mysqli_query($con, $dropdown_query);

echo '<select name="course">';
while ($course = mysqli_fetch_array($courses)) {
echo "<option value='{$course['id']}'".($student['course']==$course['id'] ? ' selected="selected"' : '').">{$course['course_name']}</option>";
} // while
echo '</select>';

How to populate dropdown menu from sql table in PHP

Try saving the result of mysqli_query:

$result = mysqli_query($db, $sql);

And then using it in the while condition:

while ($row = mysqli_fetch_array($result)) {

The select should be also "Select unitid, unitname ..." to return also the unitname used in the options:

$sql = "SELECT unitid, unitname FROM unit WHERE unitname = '$unitname'";

And you should use prepared statements if you want to prevent it from SQL injection attacks.

If you want all the units to be shown on the combo change the select to:

$sql = "SELECT unitid, unitname FROM unit";

So, the code should be now:

 <?php
$db = mysqli_connect('localhost', 'root', '', 'registration');
$sql = "Select unitid, unitname from unit";
$result = mysqli_query($db, $sql);

echo "<select name='unitid'>";
while ($row = mysqli_fetch_array($result)) {
echo "<option value='" .$row['unitid']."'> ".$row['unitname'] . "</option>";
}
echo "</select>";
?>

Populating a Dropdown Selection from MySql & Php

Heres the code thats working

//placed in beginning of code
<?php
include("database.class.php");
$database = new Database;

$result = $database->mysqlQuery("SELECT * FROM spec_tables");

?>
//Placed inside html form
<?php
echo '<select name="list">';
echo '<option>Select List</option>';
while ($row = mysql_fetch_array($result)) {
echo '<option value="' . $row['name'] .'">' . $row['name'] .'</option>';
}
echo '</select>';

?>

How to populate a drop-down box from a MySQL table in Angular 4

The best way would be to return the data as JSON. Using rxjs subscribe you can fetch the data like this

 users:any[]= [];
fetchData(){
this.http.get("users/all-users")
.subscribe((res)=>{
this.users = res.data
})

}

So in the html it would be something like

<select>
<option *ngFor="let user of users" value="user.id">
{{ user.name }}
</option>

</select>

So in your php return data like this

function getUsers(){

mysql_connect('hostname', 'username', 'password');
mysql_select_db('database-name');

$sql = "SELECT nameid FROM PC";
$result = mysql_query($sql);

return ["data"=>$result];
}

Advice though instead of using normal php use a framework like laravel/yii2 which makes returning data easy.

NB: Angular4 is a frontend framework so you cannot using echo like you would use it in a php framework.



Related Topics



Leave a reply



Submit