我创建了我的个人资料页面,显示来自MySQL的所有表格数据。所有数据都在表单和下拉列表中正确显示。但问题是选项值在选项列表中显示两次。
这是我的代码:
DEBUG JdbcTemplate - Executing prepared SQL statement [Select Account_ID,password,activated from Account where email=?]
DEBUG DataSourceUtils - Fetching JDBC Connection from DataSource
DEBUG DriverManagerDataSource - Creating new JDBC DriverManager Connection to [jdbc:mysql://db4free.net:3306/frozencat?useSSL=false]
TRACE StatementCreatorUtils - Setting SQL statement parameter value: column index 1, parameter value [admin@admin.de], value class [java.lang.String], SQL type unknown
DEBUG DataSourceUtils - Returning JDBC Connection to DataSource
DEBUG JdbcTemplate - Executing prepared SQL query
DEBUG JdbcTemplate - Executing prepared SQL statement [SELECT Account_ID,role FROM Account_Roles WHERE Account_ID=?]
DEBUG DataSourceUtils - Fetching JDBC Connection from DataSource
DEBUG DriverManagerDataSource - Creating new JDBC DriverManager Connection to [jdbc:mysql://db4free.net:3306/frozencat?useSSL=false]
TRACE StatementCreatorUtils - Setting SQL statement parameter value: column index 1, parameter value [1], value class [java.lang.String], SQL type unknown
DEBUG DataSourceUtils - Returning JDBC Connection to DataSource
INFO AuthenticationEventListener - Login attempt with username: 1 Success: true
答案 0 :(得分:6)
您需要更改以下while
代码: -
while($country = mysqli_fetch_array($result)){
//Compare User Country with country list. $row[4] is the country column in user table
if($row[4] == $country[1]){
echo "<option value='$country[1]' selected='selected'>$country[1]</option>";
}else{
echo "<option value='$country[1]'>$country[1]</option>";
}
}
注意:在您的代码中创建第一个选项,然后检查条件,这就是为什么它会显示所选选项的两倍。
答案 1 :(得分:2)
您面临的问题的正确答案由 A-2-A 提供。
<强>另外强>
您正在将所有循环选项嵌套在“#34;选择国家/地区”中。选项。您应该删除</option>
标记前的最后一个</select>
标记,然后在&#34;选择国家/地区&#34;之后移动它像这样:
<option value="">Select Country</option>
答案 2 :(得分:1)
这可以解决您的问题:
<select class="form-control" name="country" id="country">
<option value="">Select Country
<?php
//Get country list from Country master
$qry = "select * from country_master";
//Execute query
$result = mysqli_query($conn, $qry);
//Assigned fetched array to $Country
while($country = mysqli_fetch_array($result))
{
echo "<option value='$country[1]'".($row[4] == $country[1] ? " selected" : "").">$country[1]</option>";
}
?>
</option>
问题在于您已将选定选项另外回显到未选择的选项。现在,它添加了选择的&#39;如果应该选择该选项,则属性。 (根据您的情况)
答案 3 :(得分:1)
它应该像这样设置:
<select class="form-control" name="country" id="country">
<option value="">Select Country
<?php
//Get country list from Country master
$qry = "select * from country_master";
//Execute query
$result = mysqli_query($conn, $qry);
//Assigned fetched array to $Country
while($country = mysqli_fetch_array($result))
{
//Compare User Country with country list. $row[4] is the country column in user table
if($row[4] == $country[1]){
echo "<option value='$country[1]' selected='selected'>$country[1]</option>";
}
else{
echo "<option value='$country[1]'>$country[1]</option>";
}
}
?>
</option>
</select>
因为你需要检查是否选择了值,如果它没有相应地显示数据。
答案 4 :(得分:0)
您有两个问题:
示例:强>
<option value="">Select Country
</option>
<?php
//Get country list from Country master $qry = "select * from country_master";
//Execute query
$result = mysqli_query($conn, $qry);
//Assigned fetched array to $Country
while($country = mysqli_fetch_array($result)) {
if($row[4] == $country[1]) {
$selected = 'selected=""';
}
else{
$selected = "";
}
?>
<option <?php echo $selected;?> value='<?php echo $country[1];?>'>
<?php echo $country[1];?>
</option>
<?php
}
?>