gpt4 book ai didi

php - 根据select选项从mysql检索数据

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

嘿大家...我正在尝试根据从选择标记(下拉列表)中选择的选项从 mysql 数据库检索数据

这是我的 html 代码

<html>
<head>
<script>
function showUser(str)
{
if (str=="")
{
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
<select name="staff" onchange="showUser(this.value)">
<option value="">Select Staff</option>
<option value="Ms.Shakthi">Ms.Shakthi</option>
<option value="Ms.Priya">Ms.Priya</option>
</select>
</form>
<br>
<div id="txtHint"><b>Person info will be listed here.</b></div>
</body>
</html>

Getuser.php代码

<?php
$q = intval($_GET['q']);

$con = mysqli_connect('localhost','willy','12345','test');
if (!$con)
{
die('Could not connect: ' . mysqli_error($con));
}

mysqli_select_db($con,"test");
$sql="SELECT * FROM test WHERE Staff = '".$q."'";

$result = mysqli_query($con,$sql);

echo "<table border='1'>
<tr>
<th>Name</th>
<th>Class</th>
<th>Syllabus</th>
<th>Motivated</th>
<th>Time</th>
<th>Doubts</th>
<th>Marking</th>
<th>Interesting</th>
<th>Methods</th>
</tr>";

while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['Class'] . "</td>";
echo "<td>" . $row['Syllabus'] . "</td>";
echo "<td>" . $row['Motivated'] . "</td>";
echo "<td>" . $row['Time'] . "</td>";
echo "<td>" . $row['Doubts'] . "</td>";
echo "<td>" . $row['Marking'] . "</td>";
echo "<td>" . $row['Interesting'] . "</td>";
echo "<td>" . $row['Methods'] . "</td>";
echo "</tr>";
}
echo "</table>";

mysqli_close($con);
?>

当我选择一个选项时,不会检索数据

最佳答案

intval 获取一个整数,但 $_GET['q'] 是一个字符串。所以我猜你需要的是:

$q = $_GET['q'];

编辑:我建议您使用开发工具(Chrome 中的 F12 或 Firefox 中的 firebug)来调试您的程序。并 echo $variable 或使用 PHP 中的任何调试工具。

关于php - 根据select选项从mysql检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22280670/

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