douzhong3887 2014-11-13 10:36
浏览 76
已采纳

Web表单中的外键

I am creating web forms for users to enter data into my database. Now I am thinking of a good way to enter/choose foreign keys. For example I have a table A which has 1:n relation to table B. I want to create a form for table b. The user can enter the normal information in input boxes. To enter the foreign key I thought about a dropdown list, where all entries of table A are displayed. Unfortuanately, the dropdown list of HTML can only display one coulmn. So I cannot display the ID and the name of the picked entry.

Does someone know a smart way to display the name of the entry but pass the id?

Now I am doing something like this. But here only the id of the picked entry is displayed for the user. I want to display the name, but pass the id.

<select name="input_modul">
<?php 

$sql = "SELECT id, Name FROM tableA;";

if($result = $con->query($sql)) {
    $rows = $result->num_rows;
    while($row = $result->fetch_array()) {                                    
        echo "<option>". $row['id'] . "</option>";
    }                   
}
?> </select>   
  • 写回答

1条回答 默认 最新

  • dss89001 2014-11-13 10:40
    关注

    Quite simply pass $row['id'] to the value-attriubute of the option-element

    echo "<option value=\"" . $row['id'] . "\">" . $row['name'] . "</option>";
    

    This way, the value of $row['name'] will be the displayed value in the list, while $row['id'] will be passed along to do stuff with (get / post / others)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输