我正在努力在我的项目的下拉列表中显示所选选项的详细信息。
我有一个从 MYSQLi 查询填充的下拉列表。我希望用户选择一个选项,并将相关的值从数据库中提取并显示给用户。
动态填充的下拉列表用于人员的“名字”(表名称),当用户从下拉列表中选择一个名称时,我希望显示该人的记录。
下面的代码用于动态填充下拉列表。用户单击按钮并进入下一页,该页面应创建一个包含结果的表。没有错误,但也没有所需的结果。
下拉代码
<!DOCTYPE>
<html>
<head>
<title>Update Data</title>
</head>
<body>
<form name="form_update" method="post" action="update_test.php">
<?php
$con=mysqli_connect("localhost","root","","ismat_db");
//============== check connection
if(mysqli_errno($con))
{
echo "Can't Connect to mySQL:".mysqli_connect_error();
}
//This creates the drop down box
echo "<select name= 'FirstName'>";
echo '<option value="">'.'--- Please Select Person ---'.'</option>';
$query = mysqli_query($con,"SELECT FirstName FROM persons");
$query_display = mysqli_query($con,"SELECT * FROM persons");
while($row=mysqli_fetch_array($query))
{
echo "<option value='". $row['id']."'>".$row['FirstName']
.'</option>';
}
echo '</select>';
?> <input type="submit" name="submit" value="Submit"/>
</form><br/><br/>
<a href="main.html"> Go back to Main Page </a>
</body>
</html>
查看代码
<title>Update Data</title>
</head>
<body>
<!--<table>
<tr>
<td align="center"> From Database </td>
</tr>
<tr>
<td>
<table border="1">
<tr>
<td>First Name</td>
<td>Last Name </td>
<td> Gender </td>
<td> Subject </td>
<td> Hobbies </td>
</tr>
-->
<?php
$con=mysqli_connect("localhost","root","","ismat_db");
if(mysqli_errno($con))
{
echo "Can't Connect to mySQL:".mysqli_connect_error();
}
//$name = mysqli_real_escape_string($con,$_POST['select']);
// $fetch = mysqli_query($con,"SELECT * FROM persons WHERE FirstName='".$name."'");
// $row_display=mysqli_fetch_assoc($fetch);
if(isset($_POST['select']))
{
$name = mysqli_real_escape_string($con,$_POST['select']);
$fetch = "SELECT * FROM persons WHERE FirstName = '".$name."'";
$result = mysqli_query($con,$fetch);
//display the table
echo '<table border="1">'.'<tr>'.'<td align="center">'. 'From Database'. '</td>'.'</tr>';
echo '<tr>'.'<td>'.'<table border="1">'.'<tr>'.'<td>'.'First Name'.'</td>'
.'<td>'.'Last Name'.'</td>'.'<td>'. 'Gender' .'</td>'.'<td>'
. 'Subject'. '</td>'.'<td>'. 'Hobbies' .'</td>'.'</tr>';
while($data = mysqli_fetch_row($result))
{
echo ("<tr><td>$data[0]</td><td>$data[1]</td><td>$data[2]</td><td>$data[3]</td><td>$data[4] </td></tr>");
}
echo '</table>'.'</td>'.'</tr>'.'</table>';
}
?>
<!--
</table>
</td>
</tr>
</table>
-->
<br/>
<a href="update.php"> Go back to Main Page </a>
</body>
</html>