douzhenzu0247 2012-10-29 12:26
浏览 26
已采纳

来自MYSQL的php下拉菜单,如何返回其他列值?

I'm having a input form that asks for a location. The locations are stored in a mysql db and have an id (colomn: id and column: location).

I have a drop down menu that is generated from those records in the db:

    <select name="location">

    <?php                         
            $query="SELECT location FROM locations";
                $result=mysql_query($query) or die;

            while ($row=mysql_fetch_array($result)) {

            $location=$row['location'];

            echo "<option>$location</option>";
    ?>

    </select>

This all works. When the form is sumbitted, I obviously get a POST[location] for example "Belgium".

Let's say Belgium is the location in my db and has id 5, how can I return the ID as the POST variable from the dropdown box, instead of the location. Ofcourse I want the dropdown to show the locations, and not the ID's.

Thanks in advance!

  • 写回答

4条回答 默认 最新

  • dooid3005 2012-10-29 12:29
    关注

    Each option can take a value and show another string so use value="my_value" for each option inside the select tag

    <select name="location">
        <?php                         
            $query="SELECT id, location FROM locations";
            $result=mysql_query($query);
    
            while ($row=mysql_fetch_array($result)) {
                echo "<option value=\"" . $row['id'] . "\">" . $row['location'] . "</option>";
            }
        ?>
    </select>
    

    now your POST['location'] will contain the db id for selected location

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题