I have a form in my html page to insert data into the database. In the form I have drop down lists for some fields. I would like to get the data for the drop down list from the database.
Following is the code used before ( hard coded drop down )
<div class="form-group">
<label for="proj_name" class="col-sm-4 control-label">Project Name</label>
<div class="col-sm-8">
<select name="proj_name" id="proj_name" class="form-control" >
<option selected="true" value="" data-hidden="true" style="color:red">Choose Project</option>
<option value="Project1">Project1</option>
<option value="Project2">Project2</option>
<option value="Project3">Project3</option>
<option value="Project4">Project4</option>
</select>
</div>
I am using PDO to connect to the database. ( Also using bootstrap template for the form ). Now I want to populate those project names using the data in project table.
I am using following code modifying the above mentioned ones:
<div class="form-group">
<label for="proj_name" class="col-sm-3 control-label" style="color:red">Project Name</label>
<div class="col-sm-9">
<?php
try
{
$sql = "select proj_id,proj_name from tblProject";
$projresult = $db->query($sql);
$projresult->setFetchMode(PDO::FETCH_ASSOC);
}
catch (PDOException $e)
{
die("Some problem getting data from database !!!" . $e->getMessage());
}
echo '<select name="proj_name" id="proj_name" class="form-control" >';
while ( $row = $projresult->fetch() )
{
echo '<option value="'.$row['proj_name'].'">'.$row['proj_name'].'</option>';
}
echo '</select>';
?>
</div>
</div>
The form is in a bootstrap modal and when I use this code, I am unable to open the modal. Also not sure if the code is correct usage to get that data for the drop down lists.
Please correct me if am wrong anywhere. Am new to using PDO.
Any suggestions are welcomed