0

好的,所以我想在我的数据库中的区域表中插入/更新一个新区域,一个区域应该链接到一个建筑物(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);

?>
4

1 回答 1

0

这是用数据库表数据填充保管箱的代码。

<?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);
};
于 2013-04-25T01:47:41.073 回答