我正努力在项目的下拉列表中显示所选选项的详细信息。
我有一个下拉列表,它是从MYSQLi查询中填充的。我希望用户选择一个选项,并从数据库中提取相关的值并显示给用户。
动态填充的下拉列表用于人员的“FirstName”(表名),当用户从下拉列表中选择一个名称时,我希望显示该人的记录。
以下代码用于动态填充下拉列表。用户单击该按钮并转到下一页,该页面应创建包含结果的表。没有错误,但也没有要求的结果。
下拉代码
<!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>
答案 0 :(得分:2)
您在视图代码中使用了错误的字段名:
echo "<select name= 'FirstName'>";
^^^^^^^^^
v.s。
$name = mysqli_real_escape_string($con,$_POST['select']);
^^^^^^
表单以fieldname=value
对提交, NOT 输入来自的标记名称。
答案 1 :(得分:0)
建议很少
在您的查询中添加您设置的ID作为选项的值
echo "<option value='". $row['id']."'>".$row['FirstName']
所以请按如下方式更新:
$query = mysqli_query($con,"SELECT id,FirstName FROM persons");
您正在检查错误的POST var 'select'
,它应该是'FirstName',它是您在下面使用的选择字段的名称:
echo "<select name= 'FirstName'>
所以代替以下代码:
if(isset($_POST['select']))
{
$name = mysqli_real_escape_string($con,$_POST['select']);
$fetch = "SELECT * FROM persons WHERE FirstName = '".$name."'";
$result = mysqli_query($con,$fetch);
另请注意,在where子句中,您应该检查id不是firstName
使用此更正后的代码:
if(isset($_POST['FirstName']))
{
$name = $_POST['FirstName'];
$fetch = "SELECT * FROM persons WHERE id = '".$name."'";
$result = mysqli_query($con,$fetch);
答案 2 :(得分:0)
向同一页显示特定用户的数据
您的下拉框似乎
<select id="c-name" name="name" onChange="reload()">
<option value="">Select Name</option>
</select>
在Javascript中编写 reload()函数
function reload() {
var val=document.getElementById("c-name").value;
self.location="yourfilename.php?fname=" + val ;
//alert(val);
}
在页面上获取fname变量&amp;显示该变量的数据 这是从数据库中获取
在PHP中
if(isset($_REQUEST['fname'])) {
//your code
}