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

标签 php mysql ajax forms

嘿大家...我正在尝试根据从选择标记(下拉列表)中选择的选项从 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/

相关文章:

php - 如何在 PHP 中实现复制构造函数?

php - JavaScript + PHP + 动态文本字段 + Jquery

MySql重复SUM,一对多关系

php - 让脚本向 jQuery 的 .load() 方法提供 bool 值 'answer'

搜索后不显示数据库中的php数据

php - 从 Silverstripe 3.1 中的内部联接中选择多列

php - Laravel 向数据库表中插入信息时,是否可以将某个表的列留空?

mysql - 处理超过 20 条记录时出现问题

php - 如何在博客/新闻文章的文本区域中设置文本格式

c# - 从 jquery AJAX 调用 web 方法时身份验证失败