好的,所以我想在我的数据库中的区域表中插入/更新一个新区域,一个区域应该链接到一个建筑物(buildingID 是区域表中的一个 fk),目前我已经制作了一个表格,以便我可以输入与建筑物链接的新区域,但是我必须手动输入 buildingID,我想要将建筑物表中的所有 Buildingnames 显示在下拉框中,以便我可以选择它,而不是让添加新区域时输入 buildingID。
希望这是有道理的,非常感谢任何可以帮助我的人!
?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);
?>