我有一个数据库表,其中包含一些学生的记录,如下所示: table =分数id姓名分数1迈克尔·史密斯67 2露易丝·布朗72 3凯特·史密斯53 4尼克·罗斯56
我要做的是使用包含学生姓名的下拉列表来检索有关学生的信息,并一次显示一个学生。例如,如果我在下拉列表中单击Nick Ross,我希望它在表格形式中显示4、Nick Ross和56,但我得到的却是一个空白页面。请帮帮忙。以下是我的脚本
<html>
<body>
<br><br><br>
<form method = "POST" action = "students.php" >
<select name="students">
<option>Select a name:</option>
<option>Michael Smith</option>
<option>Lois Brown</option>
<option>Kate Smith</option>
<option>Nick Ross</option>
</select>
<br><br>
<input type = "submit" name "submit" value ="Process">
</form>
<br>
</body>
</html> students.php
<?php
$con = mysqli_connect('localhost','root','','school');
if(!$con) die('Could not connect: ' . mysql_error($con));
if(isset($_POST['submit'])) {
$students = $_POST['students'];
$result = mysqli_query($con,"SELECT *, FROM score WHERE students = $students");
echo "<table align='center' width='340px' border='1'>
<tr>
<th>id</th>
<th>Name</th>
<th>Score</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['score'] . "</td>";
echo "</tr>";
}
echo "</table>";
}
mysqli_close($con);
?>发布于 2015-03-16 13:37:28
如果你想显示学生记录,你可以使用简单的超链接,其中包含表单中学生的id,当用户单击该链接时,只需从url中获取id并从数据库中获取记录。
E.g
<a href="students.php?id=1">Michael Smith</a>
然后在学生页面中只获取id
<?php
$id = $_GET['id'];
//Then use simple select query to fetch record from database
SELECT * FROM score WHERE id = $id;
?>如果你想使用下拉菜单,我希望你使用Ajax,方法和我上面描述的一样。
https://stackoverflow.com/questions/29057840
复制相似问题