繁体   English   中英

根据选择选项从mysql检索数据

[英]Retrieve data from mysql based on select option

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

这是我的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中的任何调试工具。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM