0
好的,我想插入/更新一個新區域到我的數據庫的區域表中,一個區域應該鏈接到一個建築物(buildingID是一個fk in區表),目前我已經做了一個表格,以便我可以進入與建築物鏈接的新區域,但是我必須手動鍵入buildingID,我想要的是從建築物表格中的所有建築物名稱顯示在下拉框中,以便我可以選擇它,而不必在添加新區域時鍵入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);
?>
非常感謝! – user2317823 2013-04-26 15:32:56
別忘了! mysql_query已被棄用。考慮使用PDO或mysqli。我寧願PDO。 – 2013-04-26 20:06:33