Php 使用选择框获取将用于添加为外键id的id
我正忙着为我的管理页面编程。在我的“一页”中,我使用了一个选择框,管理员将首先选择要为其添加区域的中心,然后再添加区域。当管理员提交表单时,信息将插入mysql表中,该表有以下列-id、name、fk_hub_id&active 我从来没有使用过外键,这对我来说是非常新的,因为我也是一个初学者。以下是我目前的代码:Php 使用选择框获取将用于添加为外键id的id,php,mysql,Php,Mysql,我正忙着为我的管理页面编程。在我的“一页”中,我使用了一个选择框,管理员将首先选择要为其添加区域的中心,然后再添加区域。当管理员提交表单时,信息将插入mysql表中,该表有以下列-id、name、fk_hub_id&active 我从来没有使用过外键,这对我来说是非常新的,因为我也是一个初学者。以下是我目前的代码: <?php // Start the session require_once('startsession.php'); require_once('con
<?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>
地区
将下拉元素包含到表单中,并在$\u 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> </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> </td>
<tr>
<td> </td>
<td><input type="submit" name="submit" value="Add" align= "right" /></td>
</tr>
</table>
这是对的吗?我的下拉列表不见了。@Alwina是的,你能帮我做一下insert语句吗?@Alwina我想你错过了空格字符。使用$sql=“INSERT INTO区域
(name)值(“$name”)”;而不是$sql=“插入区域
(名称)值(“$name”)”;。然后我必须为外键执行单独的update语句吗?