2013-07-29 16 views
1

我正忙于编辑我的管理页面。在我的一个页面中,我正在使用一个选择框,管理员将首先选择要为其添加区域的集线器,然后再添加该区域。当管理员提交表单时,信息将被插入到具有以下列的mysql表中 - id,name,fk_hub_id & active。使用选择框获取将用作添加为外键ID的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的值?我知道我仍然需要更改我的插入语句。

回答

0

将您的下拉元素包含到您的表单中,并在$_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> 
+0

是这样的​​<?= $ dropdown; ?>对不对?我的下拉列表已经消失。 – Alwina

+0

@Alwina是的,'<?='只是'<?的快捷方式? echo'。可能是你的设置不允许你使用'<?'shorcuts。我编辑了一个代码。 – zavg

+0

你可以帮我插入语句吗? – Alwina