2010-10-17 109 views
0

我學習AJAX,我試圖重新創建這個非常簡單的例子:阿賈克斯從MySQL中檢索數據 - 工作不正常

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

I R實現這可能是一個非常基本的錯誤,但我找不到解決方法。

對不起,我不得不粘貼這麼多的代碼,但我沒有看到任何其他方式,我可以解釋我的問題,你可以幫助我。

非常感謝!

回答

4

您正在使用

FirstName LastName 

代替

Firstname Lastname 

數組索引是區分大小寫的。

+0

OMG我真的打我的屏幕右知道,該死的PHP不區分大小寫的狂熱。非常感謝你!更多的問題解決了! – Trufa 2010-10-17 14:02:51

+0

順便說一句,在我看到那個錯誤之前,冰帽會融化!再次感謝! – Trufa 2010-10-17 14:05:09

+0

@Trufa @Trufa沒關係 - 從經驗來看,它只會發生一兩次,然後將它存儲在你本能的記憶中:) – 2010-10-17 14:07:25