2015-12-03 46 views
1

查看使用AJAX的信息,我有這樣的可以添加,編輯,刪除和查看。它工作正常,當我把所有的代碼放在一個文件中的代碼..但我想單獨擁有它。 。我唯一的問題是「查看」部分..當你添加一個單詞時,它必須自動查看所有的單詞..以及編輯,刪除..即使我已經嘗試搜索一個單詞,仍然無法正常工作。林在這仍然是新..請幫助:/不能在MySQL

這是觀察的話代碼。這種代碼是在我的add.html,delete.html和edit.html

<form> 
     <div> 
      Word: 
      <input type="text" name="viewWords" onKeyUp="showWord(this.value)" /> 
     </div> 
    <center> 
     <input name="View" type="button" value="View" onClick="showWord(viewWords.value)" /> 
    </center> 
    </form> 

PHP文件中查看所有單詞

<?php 
$q=$_GET["q"]; 
$con = mysql_connect('localhost', 'root', ''); 
if (!$con) 
{ 
die('Could not connect: ' . mysql_error()); 
} 

mysql_select_db("dictionary", $con); 

$sql="SELECT * FROM english WHERE word like '".$q."%'"; 

$result = mysql_query($sql); 

echo "<table border='1'> 
<tr> 
<th>Word</th> 
<th>Meaning</th> 
</tr>"; 

while($row = mysql_fetch_array($result)) 
{ 
    echo "<tr>"; 
    echo "<td>" . $row['word'] . "</td>"; 
    echo "<td>" . $row['meaning'] . "</td>"; 
    echo "</tr>"; 
} 
    echo "</table>"; 

mysql_close($con); 
?> 

如果要查看

function showWord(word) 
{ 
    xmlHttp=GetXmlHttpObject(); 
    if (xmlHttp==null) 
{ 
    alert ("Browser does not support HTTP Request"); 
    return; 
} 
    var url="search.php"; 
    url=url+"?q="+word; 
    xmlHttp.onreadystatechange=stateChanged; 
    xmlHttp.open("GET",url,true); 
    xmlHttp.send(null); 
} 

回答

0

既然你解決使用按鍵的變量js文件而不是簡單的數組索引,切換mysql_fetch_array與mysql_fetch_assoc()同樣當你想從你喜歡這個

while(($r = mysql_fetch_assoc(..)) !=FALSE){ 
print_r($r); 
} 
+0

循環仍不能正常運行的SQL查詢檢索多行:'( – yumi