Why is it not picking up the items I want from the table in the DB instead its putting YEAR as the item in the column of the table.
enter image description hereBudget Year:enter image description here
<?php
echo "<select name= 'Budget_Year' class='form-control selectpicker' onChange='getState(this.value)' Required>";
$default = "year";
$time = array('year'=>"2018-2019");
foreach($time as $key=>$val)
{
echo ($key == $default) ? "<option selected=\"selected\" value=\"$key\">$val</option>":"<option value=\"$key\">$val</option>";
}
$sql = "SELECT budget_year FROM Budget_Year";
$query = sqlsrv_query($conn,$sql);
$query_display = sqlsrv_query($conn,$sql);
while($row=sqlsrv_fetch_array($query_display,SQLSRV_FETCH_ASSOC))
{
echo '<option value=" '. $row['budget_year'].' ">'.$row['budget_year']. '</option>';
continue;
}
?>
</td>
答案 0 :(得分:0)
I'm not sure what you're trying to do.
Here in your code (as in your database) the only thing which is submitted through the form and store in your DB, will be the string "year".
When you make a $time = array('year'=>"2018-2019");
"year" is the key, and "2018-2019" is the value.
If you write:
$default = "year";
$time = array('year'=>"2018-2019");
foreach($time as $key=>$val)
{
echo ($key == $default) ? "<option selected=\"selected\" value=\"$key\">$val</option>":"<option value=\"$key\">$val</option>";
}
as there is only one pair of key/value in your array, the code above it's striclty the same as just writing:
echo "<option selected=\"selected\" value=\"year\">2018-2019</option>";
And then, after this listing your DB results. And as there is only records in the DB with "year" in the column Budget_Year
, your select list will result in:
<select name= 'Budget_Year' class='form-control selectpicker' onChange='getState(this.value)' Required>
<option selected="selected" value="year">2018-2019</option>
<option value="year">year</option>
<option value="year">year</option>
<option value="year">year</option>
</select>
When storing the result with the form for adding a new entry, you should consider using $val
in the value arg of your option, not $key
:
<option selected=\"selected\" value=\"$val\">