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

来自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 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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀