使用选择框获取一个id,该id将用于添加为外键id


Using a select box to obtain an id which will be use to add as foreign key id

我正忙于编程我的管理页面。在我的一个页面中,我使用了一个选择框,管理员将首先选择要添加区域的集线器,然后再添加区域。当管理员提交表单时,信息将被插入到mysql表中,该表具有以下列:id、name、fk_hub_id&忙碌的

我从来没有使用过外键,这对我来说是非常新鲜的,因为我也是一个初学者。这是我到目前为止的代码:

  <?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>&nbsp;</td>
    <tr>
        <td>Area:</td>
        <td><input type="text" name="name" align= "right"/></td>
    </tr>
    <tr><td>&nbsp;</td>
    <tr>
        <td>&nbsp;</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的值?我知道我仍然需要更改插入语句。

将下拉元素包含到表单中,并在$_POST['hub']变量中接收其值。

<?php      
    $dropdown = "<select name='hub'>";
    while($row = mysql_fetch_assoc($result)) {
    $dropdown .= "'r'n<option value='{$row['name']}'>{$row['name']}</option>";
    }
    $dropdown .= "'r'n</select>";
?>
<form method="post" align= "right">
<table >
    <tr><td>&nbsp;</td>
    <tr>
        <td>Area:</td>
        <td><input type="text" name="name" align= "right"/></td>
    </tr>
    <tr>
       <td>Select the Hub for which you want to insert an area: </td>
       <td><?php echo $dropdown; ?></td>
    </tr>
    <tr><td>&nbsp;</td>
    <tr>
        <td>&nbsp;</td>
        <td><input type="submit" name="submit" value="Add" align= "right" /></td>
    </tr>
</table>