简体   繁体   中英

Using PHP and MySQL how do I populate one table using some field values from another table

I am working on a database project on which one of the forms table need to be filled with some values from another table using a select box.

Table one is called reg_table and has the following fields:

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

and table two is called valparts_table and has the following together will all the fields of table one as seen below

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. Now when you open the valpartsfrm form and select a Surname value using a select box object on the valpartsfrm form that is linked to reg_table, it will show all the Surnames. When you select a Surname from the list, it should fill the following filds on the valpartsfrm with the values of RegNo Surname Othernames Assembly from the reg_table.

  2. Once this is done, you then manually fill the remaining valpartsfrm form fields (MTNumber, Camping Hostel)

Please note that the valparts_table is bound to the valpartsfrm form.

The link http://dbms.rmww.org/images/validatepart.jpg shows a sample of the database developed using MS Access 2007

i thing you want to fill your form as you select the option try this code

its form demo

in this select box has option value which in json string and a onchange function

<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

this js function call when you change the select box option in this json string is deoceded in object and fill the textboxes accordingly

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM