gpt4 book ai didi

php - 使用选择框获取将用于添加为外键 ID 的 ID

转载 作者:行者123 更新时间:2023-11-30 23:15:47 24 4
gpt4 key购买 nike

我正忙于编写我的管理页面。在我的一页中,我使用了一个选择框,管理员将首先选择他们要为其添加区域的集线器,然后再添加该区域。当管理员提交表单时,信息将被插入到 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>&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>

关于php - 使用选择框获取将用于添加为外键 ID 的 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17924058/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com