【发布时间】:2023-03-26 12:06:01
【问题描述】:
我创建了一个数据库,其中包含 3 个表,分别是 spusername、splocation、sprecord。 spusername 有 id、splocation_id、lastname、firstname。我希望能够有一个下拉菜单,它从数据库中提取了 id、lastname、firstname,并且在下拉菜单中它只显示了一个包含 lastname、firstname 的所有名称的列表。然后,一旦我选择了一个人,我就会有另一个下拉菜单,其中包含培训类型。然后当我点击提交时,它将在另一个表中生成一条记录,其中包含人员 ID 和培训记录。因此,当我进行搜索时,它会提取该人的用户和培训记录....一个可以做我想做的搜索,但我从来没有做一个数据条目做一个下拉,它有从数据库生成的值。
编辑代码:在 Vegard 编码的帮助下,我得到了这个,现在经过几次试验和错误,它工作得很好。谢谢!
代码:
<?php
if (isset($_REQUEST['Submit'])) {
$sql = "INSERT INTO $db_table(spusername_id,sptraining_id) values ('".mysql_real_escape_string(stripslashes($_REQUEST['spusername_id']))."','".mysql_real_escape_string(stripslashes($_REQUEST['sptraining_id']))."')";
if($result = mysql_query($sql ,$db)) {
echo '<h1>Thank you</h1>Your information has been entered into the database<br><br>';
} else {
echo "ERROR: ".mysql_error();
}
} else {
?>
<h1>Add Training Information To Database</h1><hr>
<br><br>
<form method="post" action="">
<select name="spusername_id">
<option value="default">Select Employee</option>
<?php
include("connectspusers.php"); /*file where you have stored your DB conn. settings*/
$result = mysql_query('SELECT id, lastname, firstname FROM spusername ORDER BY lastname ASC') or die (mysql_error());
while ($row = mysql_fetch_array($result)) {
echo '<option value="' . $row['id'] . ' ' . $row['lastname'] . ' ' . $row['firstname'] . '">' . $row['lastname'] . ', ' . $row['firstname'] . '</option>';
}
?>
</select>
<select name="sptraining_id">
<option value="default">Select Training</option>
<?php
include("connectsptraining.php"); /*file where you have stored your DB conn. settings*/
$result = mysql_query('SELECT id, trainingtype, level FROM sptraining ORDER BY level ASC') or die (mysql_error());
while ($row = mysql_fetch_array($result)) {
echo '<option value="' . $row['id'] . ' ' . $row['trainingtype'] . ' ' . $row['level'] . '">' . $row['trainingtype'] . ' - ' . $row['level'] . '</option>';
}
?>
</select>
<br><br>
<input type="submit" name="Submit" value="Submit">
</form>
<?php
}
?>
【问题讨论】:
-
这里似乎是一个完整的任务。我们在这里为您提供帮助,如果您有任何问题,请粘贴您的代码??