根据select选项从mysql中检索数据

时间:2014-03-09 09:42:24

标签: php mysql ajax forms

嘿伙计们...我正在尝试根据从select标签(下拉列表)中选择的选项从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);
?>

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

1 个答案:

答案 0 :(得分:0)

intval得到一个整数,但$ _GET ['q']是一个字符串。所以,我满足你的需求是:

$q = $_GET['q'];

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