简体   繁体   中英

inserting data from one table into another from drop-down using foreign keys

Ok so I want to insert/update a new zone into the zones table in my database, a zone should be linked to a building (buildingID is a fk in the zones table), at the moment I have made a form so that I can enter a new zone linked with a building, however I have to manually type in the buildingID, what I want is for all of the Buildingnames from the buildings table to be shown in a drop-down box so I can select it, rather than having to type in the buildingID when adding a new zone.

hope this makes sense, many thanks to anyone who can help me!

?php
$con = mysql_connect ("localhost","user","pass");
if (!$con){
die("can not connect: " . mysql_error());
}
mysql_select_db("dbname",$con);

if(isset($_POST['update'])){
$updatequery = "UPDATE zones SET Zonename='$_POST[zonename]', Zonenumber='$_POST[zonenumber]', Zonecapacity='$_POST[zonecapacity]', buildingID='$_POST[buildingid]'WHERE zoneID='$_POST[hidden]'";
mysql_query($updatequery, $con);
};

if(isset($_POST['add'])){
$addquery = "INSERT INTO zones (Zonename, Zonenumber, Zonecapacity, buildingID) VALUES       ('$_POST[uname]', '$_POST[ucapacity]', '$_POST[unumber]', '$_POST[ubuilding]')";
mysql_query($addquery, $con);
};


$sql = "SELECT * FROM zones";

$myData = mysql_query($sql,$con);
echo "<table>
<tr>
<th> </th>
<th>Zone Name</th>
<th>Zone Number</th>
<th>Zone Capacity</th>
<th>Building ID</th>
</tr>";
while ($record = mysql_fetch_array($myData)){
echo "<form action=addeditzone.php method=post>";
echo " <tr>";
echo "<td>" . $record['zoneID'] . " </td>";
echo "<td>" . "<input type=text name=zonename value='" . $record['Zonename'] . "' </td>";
echo "<td>" . "<input type=text name=zonenumber value='" . $record['Zonenumber'] . "' </td>";
echo "<td>" . "<input type=text name=zonecapacity value='" . $record['Zonecapacity'] . "'  </td>";
echo "<td>" . "<input type=text name=buildingid value='" . $record['buildingID'] . "'    </td>";
echo "<td>" . "<input type=hidden name=hidden value=" . $record['zoneID'] . " </td>";
echo "<td>" . "<input type=submit name=update value=update" . " </td>";
echo "</form>";
}
echo "<form action=addeditzone.php method=post>";
echo "<tr>";
echo "<td><td><input type=text name=uname></td>";
echo "<td><input type=text name=unumber></td>";
echo "<td><input type=text name=ucapacity></td>";
echo "<td><input type=text name=ubuilding></td>";
echo "<td>" . "<input type=submit name=add value=add>" . " </td>";
echo "<tr>";
echo "</form>";
echo "</table>";
mysql_close($con);

?>

Here is the code to fill a dropbox with database table data.

<?php
    $con = mysql_connect ("127.0.0.1","root");
    if (!$con){
        die("can not connect: " . mysql_error());
    }
    mysql_select_db("examples",$con);
if (isset($_POST['submit'])){




    $sql = "INSERT INTO zones (Zonename,Zonecapacity,buildingID) VALUES
    ('$_POST[Zonename]','$_POST[Zonecapacity]','$_POST[BuildingID]')";
    $inserted=mysql_query($sql,$con);
    if($inserted) {
        echo "New Zone succesfully entered";
    } else {
        echo "fail ". mysqli_error($con);
    };
} else {
    echo "<form action='insertzone1.php' method='post'>
            Insert New Zone:<br><br>
            Zone Name:<br><input type='text' name='Zonename'><br><br>
            Zone Capacity:<br><input type='int' name='Zonecapacity'><br><br>
            Building:<br>";
    $sql = "select * from building";
    $res = mysql_query($sql,$con);
    echo mysql_error($con);
    echo "<select name='BuildingID'>";
    while ($row = mysql_fetch_array($res)){

        echo "<option value=" . $row['buildingID'] . ">". $row['buildname'] . "</option>";
    }
    echo "</select><br><input type='submit' name='submit'>
            </form>";


    mysql_close($con);
};

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