2013-08-16 57 views
0

嗨,我使用用ajax檢索數據庫MySQL的數據不能正常工作

在我使用的代碼的HTML下拉的onchange事件,應該得到的地址列的值時,我改變 下拉。

但它不工作。可能出了什麼問題?

這裏是代碼

<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(); 
     } 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>  
    <?php 
    mysql_connect('localhost', 'tiger', 'tiger'); 
    mysql_select_db('theaterdb'); 
    $sql = "select theater_name from theater;"; 
    $result = mysql_query($sql); 
    echo "<select name='theater_name' id='course' onchange='showUser(this.value);'>"; 
    while ($row = mysql_fetch_array($result)) { 
     echo "<option value='" . $row['theater_name'] ."'>" . $row['theater_name']. "</option>"; 
    } 
    echo "</select>"; 
?> 

</form> 

<br> 
<div id="txtHint"><b>Info</b></div> 
</body> 
</html> 

代碼getuser.php

<?php 
    $q = $_GET["q"]; 
    $con = mysqli_connect("localhost", "tiger", "tiger", "theaterdb"); 
    if (!$con) { 
     die('Could not connect: ' . mysqli_error($con)); 
    } 

    mysqli_select_db($con); 
    $sql = "SELECT address FROM theater WHERE theater_name = '".$q."'"; 

    $result = mysqli_query($con, $sql); 

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

    while($row = mysqli_fetch_array($result)) { 
     echo "<tr>"; 
      echo "<td>" . $row['address'] . "</td>"; 
     echo "</tr>"; 
    } 
    echo "</table>"; 
    mysqli_close($con); 
?> 
+0

你得到的'$ result'的東西嗎? 'print_r($ result)' 而'$ q'? 'echo $ q' –

+0

首先解釋「Not working」 – Anigel

+0

請確保提供的用戶名和密碼正確 –

回答

2

好吧,我過輕微改動的文件,你不應該使用mysql_或mysqli_功能更多,只是不...你肯定不應該使用MySQL的函數在一個文件和另一個mysqli函數...我已經切換到使用PDO,你現在的腳本不容易SQL注入,並且據我所知它工作得很好。

的index.html

<html> 
    <head> 
     <script> 
      function showUser(str) { 
       if(str=="") { 
        document.getElementById("txtHint").innerHTML=""; 
        return; 
       } 

       if(window.XMLHttpRequest) { 
        xmlhttp=new XMLHttpRequest(); 
       } else { 
        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> 
     <?php 
     try { 
      $dbh = new PDO('mysql:dbname=theaterdb;host=localhost','tiger','tiger'); 
     } catch (PDOException $e) { 
      echo 'Connection failed: ' . $e->getMessage(); 
     } 

     $sql = "SELECT theater_name FROM theater;"; 

     $sth = $dbh->prepare($sql); 
     $sth->execute(); 

     echo "<select name='theater_name' id='course' onchange='showUser(this.value);'>"; 

     while($row = $sth->fetch(PDO::FETCH_ASSOC)) { 
      echo "<option value='" . $row['theater_name'] ."'>" . $row['theater_name']. "</option>"; 
     } 
     echo "</select>"; 
     ?> 
     </form> 
     <br> 
     <div id="txtHint"><b>Info</b></div> 
    </body> 
</html> 

getuser.php

<?php 
$q = strtolower(trim($_GET["q"])); 

try { 
    $dbh = new PDO('mysql:dbname=theaterdb;host=localhost','tiger','tiger'); 
} catch (PDOException $e) { 
    echo 'Connection failed: ' . $e->getMessage(); 
} 

$sql = 'SELECT address FROM theater WHERE LOWER(theater_name) = :q'; 

$sth = $dbh->prepare($sql); 
$sth->bindValue(':q', $q); 
$sth->execute(); 

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

while($row = $sth->fetch(PDO::FETCH_ASSOC)) { 
    echo "<tr>"; 
    echo "<td>" . $row['address'] . "</td>"; 
    echo "</tr>"; 
} 

echo "</table>"; 

$dbh = null; 
+0

Thanks @Garry Welding –