使用PHP和MySQL如何使用另一个表中的某些字段值填充一个表

时间:2014-01-16 09:27:05

标签: php mysql

我正在开发一个数据库项目,其中一个表单表需要使用选择框从另一个表中填充一些值。

表一被称为reg_table,其中包含以下字段:

RegNo | Surname | Othernames | Assembly
-------------------------------------------------
00001 | John    | Okon       | Glory Assembly
00002 | Peter   | Dan        | Shepherds Assembly
00003 | Ada     | Victor     | Pnuema Assembly

和表格2被称为valparts_table,并且如下所示,表一的所有字段如下所示

CountID  | MTNumber | Camping | Hostel    |  RegNo | Surname | Othernames | Assembly
----------------------------------------------------------------------------------------------
00001    | 0002     | Yes     | Hostel-1  |  00001 | John    | Okon       | Glory Assembly
00002    | 0003     | No      | Hostel-2  |  00002 | Peter   | Dan        | Shepherds Assembly
00003    | 0004     | Yes     | Hostel-3  |  00003 | Ada     | Victor     | Pnuema Assembly
  1. 现在,当您打开valpartsfrm表单并使用链接到reg_table的valpartsfrm表单上的选择框对象选择Surname值时,它将显示所有Surnames。当您从列表中选择一个姓氏时,它应该在valpartsfrm上填写以下filds,其中包含reg_table中RegNo Surname Othernames Assembly的值。

  2. 完成后,您手动填写剩余的valpartsfrm表单字段(MTNumber,Camping Hostel)

  3. 请注意,valparts_table绑定到valpartsfrm表单。

    链接http://dbms.rmww.org/images/validatepart.jpg显示使用MS Access 2007开发的数据库样本

1 个答案:

答案 0 :(得分:0)

我想在您选择选项时填写表单 试试这段代码

其表单演示

此选择框中的

具有选项值,该值在json字符串和onchange函数

<table>
<tr>
    <td>Select Surname</td>
    <td>
        <select id="my_select" onchange="javascript:fill_form(this.value)">
            <option value="">Select</option>
            <?php
            $res = mysql_query("SELECT * FROM `reg_table` ") or die(mysql_error());
            while($row = mysql_fetch_assoc($res))
            {
              $key = json_encode($row);
              echo "<option value='".$key."'>".$row['Surname']."</option>";
            }
            ?>
        </select>
    </td>
</tr>

<tr>
    <td>RegNo</td>
    <td>
        <input type="text" value="" name="RegNo" id="RegNo" />
    </td>
</tr>

<tr>
    <td>Surname</td>
    <td>
        <input type="text" value="" name="Surname" id="Surname" />
    </td>
</tr>

<tr>
    <td>Othernames</td>
    <td>
        <input type="text" value="" name="Othernames" id="Othernames" />
    </td>
</tr>

<tr>
    <td>Assembly</td>
    <td>
        <input type="text" value="" name="Assembly" id="Assembly" />
    </td>
</tr>
</table>

<强> JS

更改选择框选项时,此js函数调用 在这个json字符串中取消对象并相应地填充文本框

<script>
function fill_form(json_string)
{   

   if(json_string=="")
   {
        document.getElementById('RegNo').value = "";
        document.getElementById('Surname').value = "";
        document.getElementById('Othernames').value = "";
        document.getElementById('Assembly').value = "";
   }
   else
   {
        var jsonData = JSON.parse(json_string); // json string to object 
        document.getElementById('RegNo').value = jsonData.RegNo;
        document.getElementById('Surname').value = jsonData.Surname;
        document.getElementById('Othernames').value = jsonData.Othernames;
        document.getElementById('Assembly').value = jsonData.Assembly;
   }
}
</script>