我正忙于编写我的管理页面。在我的一个页面中,我使用了一个选择框,管理员将首先选择他们想要为其添加区域的集线器,然后再添加该区域。当管理员提交表单时,信息将被插入到 mysql 表中,该表具有以下列 - id、name、fk_hub_id 和 active。
我从来没有使用过外键,这对我来说很新,因为我也是一个初学者程序员。到目前为止,这是我的代码:
<?php
// Start the session
require_once('startsession.php');
require_once('connectvars.php');
?>
<!DOCTYPE html PUBLIC >
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Area</title>
</head>
<body>
<?php
require_once('header.php');
include('menu.php');
// Write out our query.
$query = "SELECT * FROM hub";
// Execute it, or return the error message if there's a problem.
$result = mysql_query($query) or die(mysql_error());
$dropdown = "Select the Hub for which you want to insert an area: <select name='hub'>";
while($row = mysql_fetch_assoc($result)) {
$dropdown .= "\r\n<option value='{$row['name']}'>{$row['name']}</option>";
}
$dropdown .= "\r\n</select>";
echo $dropdown;
?>
<form method="post" align= "right">
<table >
<tr><td> </td>
<tr>
<td>Area:</td>
<td><input type="text" name="name" align= "right"/></td>
</tr>
<tr><td> </td>
<tr>
<td> </td>
<td><input type="submit" name="submit" value="Add" align= "right" /></td>
</tr>
</table>
<?php
if (isset($_POST['submit']))
{
// Connect to the database
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$hub_name = $_GET['id'];
$name=trim($_POST['name']) ;
$active=trim($_POST['active']);
$mistakes = array();
if (empty($name) || (!(ctype_alpha($name)))) {
$mistakes[] = 'ERROR - Your title is either empty or should only contain ALPHABET CHARACTERS.';
}
else
{
// accept title and sanitize it
$name = mysql_real_escape_string(stripslashes($_POST['name']));
}
if (sizeof($mistakes) > 0) {
echo "<ul>";
foreach ($mistakes as $errors)
{
echo "<li>$errors</li>";
//echo "<a href='areatitle_index.php'>Back...</a>";
}
echo "</ul>";
echo '<br />';
}
else {
$sql ="INSERT INTO `area`(name) VALUES ('$name')";
$result = mysql_query($sql);
if($result) {
//echo "<a href='menu.php'>Back to main menu</a>";
//echo "<BR>";
}
else{
echo "ERROR - The same Title already exist in the database";
}
}
}
?>
</form>
<table class="table2" >
<br>
<tr>
<th> ID </th>
<th> Area </th>
<th> Active </th>
<th colspan = '1' > </th>
</tr>
<?php
// Connect to the database
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$result=mysql_query("SELECT * FROM area ");
while($test = mysql_fetch_array($result))
{
$id = $test['id'];
echo "<tr align='center'>";
echo"<td><font color='black'>" .$test['id']."</font></td>";
echo"<td><font color='black'>" .$test['name']."</font></td>";
echo"<td><font color='black'>" .$test['active']."</font></td>";
echo"<td> <a href ='area_view.php?id=$id'>Edit</a>";
echo "</tr>";
}
// close connection
mysql_close();
?>
</table>
</body>
</html>
我希望我走在正确的轨道上。我的问题是 - 如何从我的选择框选项中获取 id 作为我的 fk_hub_id 的值?我知道我仍然需要更改我的插入语句。