gpt4 book ai didi

php - Ajax 从 Mysql 检索数据 - 无法正常工作

转载 作者:太空宇宙 更新时间:2023-11-03 11:17:40 25 4
gpt4 key购买 nike

我正在学习 AJAX,我正在尝试重新创建这个相当简单的示例:

http://www.w3schools.com/php/php_ajax_database.asp

我创建了这个数据库:

data1

alt text

现在要重新创建示例,我基本上只是复制并粘贴了代码:

index.html

<html>
<head>
<script type="text/javascript">
function showUser(str)
{
if (str=="")
{
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
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="users" onchange="showUser(this.value)">
<option value="">Select a person:</option>
<option value="1">Juan</option>
<option value="2">Manuel</option>
</select>
</form>
<br />
<div id="txtHint"><b>Person info will be listed here.</b></div>

</body>
</html>

和getuser.php

<?php
$q=$_GET["q"];

$con = mysql_connect('localhost', 'root', 'root');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("ajax_demo", $con);

$sql="SELECT * FROM user WHERE id = '".$q."'";

$result = mysql_query($sql);

echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";

while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td>" . $row['Age'] . "</td>";
echo "<td>" . $row['Hometown'] . "</td>";
echo "<td>" . $row['Job'] . "</td>";
echo "</tr>";
}
echo "</table>";

mysql_close($con);
?>

现在奇怪的是,当我转到 index.html 时,我可以看到年龄、家乡和工作,但看不到名字和姓氏:

alt text

我意识到这可能是一个非常基本的错误,但我找不到解决方法。

很抱歉,我不得不粘贴这么多代码,但我没有看到任何其他方式可以解释我的问题,您可以帮助我。

提前致谢!

最佳答案

您正在使用

FirstName  LastName

代替

Firstname  Lastname

数组索引区分大小写。

关于php - Ajax 从 Mysql 检索数据 - 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3953504/

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