doukun5339
doukun5339
2017-02-02 21:17
浏览 84
已采纳

来自mysql数据库的下拉菜单

i need to populate a dropdown menu with date from database, this is what i've done so far

<div class="col-md-6">
    <?php
        $query_user_group = mysqli_prepare ($conn, "
SELECT group_name
     , group_id 
  FROM user_group_join 
  LEFT
  JOIN user_group 
    ON user_group_join . group_join_id = user_group . group_id 
 WHERE user_join_id = ?
");
       mysqli_stmt_bind_param($query_user_group, 'i', $client_id);
       mysqli_stmt_execute($query_user_group);
       mysqli_stmt_bind_result($query_user_group, $group_name, $group_id);
       mysqli_stmt_fetch ($query_user_group);
       mysqli_stmt_close($query_user_group);

    ?>

                        <div class="form-group">
                        <label class="control-label">Condominio in gestione*</label>
                        <select class="bs-select form-control" name="usergroup">
                        <option value="<?php echo $group_id;?> " selected="selected"><?php echo $group_name;?></option>

                        <?php 

                        $select_group_query= mysqli_prepare($conn, "SELECT group_id, group_name FROM user_group");  
                        mysqli_stmt_execute($select_group_query);
                        mysqli_stmt_bind_result($select_group_query, $idgruppo, $nomegruppo);


                            while(mysqli_stmt_fetch($select_group_query))

                                {     

                                    echo "<option value= '".$idgruppo."'>" . $nomegruppo . "</option>";

                                }


                        ?>  

                        </select>

                        <span class="help-block"> Assicurati di aver creato una scheda condominio! <br>Per inserire un nuovo condominio <a href="admin_create_new_group.php">Clicca Qui</a></span>



                        </div>
</div>

The problem is that the dropdown shows the selected value twice in the dropdown, any idea how i can sort it out? This is a screenshot of the error enter image description here Many thanks

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongpouda6700
    dongpouda6700 2017-02-02 21:33
    已采纳

    You can use DISTINCT in your SELECT query, like this:

    SELECT DISTINCT group_id, group_name FROM ...
    

    Also, you don't need this line at all,

    <option value="<?php echo $group_id;?> " selected="selected"><?php echo $group_name;?></option>
    

    You can use the selected attribute of option tag to achieve the desired result.

    So your dropdown list code should be like this:

    <select class="bs-select form-control" name="usergroup">
    <?php 
        $select_group_query= mysqli_prepare($conn, "SELECT DISTINCT group_id, group_name FROM user_group");  
        mysqli_stmt_execute($select_group_query);
        mysqli_stmt_bind_result($select_group_query, $idgruppo, $nomegruppo);
        while(mysqli_stmt_fetch($select_group_query)){ 
            $output = "<option value= '".$idgruppo."'";
            if($idgruppo == $group_id){
                $output .= " selected='selected'";
            }
            $output .= ">" . $nomegruppo . "</option>";
            echo $output;
        }
    ?>  
    </select>
    
    点赞 评论

相关推荐