2017-08-20 2 views
0

ライブアヤックス検索をしようとしています。単語を入力すると、w3schoolと同様に自動的に候補が表示されます。しかし、何らかの理由で私のインデックスファイルとPHPがデータ値を交換しなかったり、何らかの理由でデータベースが接続しなかったりします。私がいつも得意とするのは「国が見つかりません」です。コードのエラーをチェックできますか? これは、PHPファイルです:Ajax検索でデータベースに接続しません

<?php 
include_once('dbconnect.php'); 
$q = intval($_GET['q']); 
    $query = mysqli_query($conn,"SELECT * FROM `users` WHERE userCountry LIKE '%".$q."%'"); 
     $count = mysqli_num_rows($query); 
//Replace table_name with your table name and `thing_to_search` with the column you want to search 
    if($count == "0" || $q == ""){ 
     $s[] = "No Country found!"; 
    }else{ 
       while($row = mysqli_fetch_array($query)){ 
     $s[] = $row['userCountry']; // Replace column_to_display with the column you want the results from 
       } 
     } 
for($x = 0; $x < $count; $x++) { 
      echo $s[$x]; 
      echo "<br>"; 
} 
?> 

、これが私のindex.phpファイルです:

<head> 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
     <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> 
     <link rel="stylesheet" href="/resources/demos/style.css"> 
     <script src="https://code.jquery.com/jquery-1.12.4.js"></script> 
     <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> 
<script> 
function showCountry(str) { 
    if (str == "") { 
     document.getElementById("txtHint").innerHTML = ""; 
     return; 
    } else { 
     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 (this.readyState == 4 && this.status == 200) { 
       document.getElementById("txtHint").innerHTML = this.responseText; 
      } 
     }; 
     xmlhttp.open("GET","indexsearchquery.php?q="+str,true); 
     xmlhttp.send(); 
    } 
} 
</script> 
<input id="search-box" name="q" type="text" autocomplete="off" placeholder="Search country..." onchange="showCountry(this.value)" /> 
     <input type='image' name='search' id="search-icon" value='Submit' src="search-icon.png" > 
     <p style="color:white;">Suggestions: <span id="txtHint" ></span></p> 
+0

デバッグするときに、サーバーに送信される値は何ですか?実行時に実行される実際のSQLクエリは何ですか? 「ユーザー」のデータは何ですか?どのレコードが返されると思いますか? – David

答えて

0

あなたの国名うではない整数インチしかし、あなたは、それデータベース接続変数を含む1のでdbconnect.phpファイルの内容を知ることが重要である

include_once('dbconnect.php'); 
$q = $_GET['q']; //<-----remove intval 
    $query = mysqli_query($conn,"SELECT * FROM `users` WHERE userCountry LIKE '%".$q."%'"); 
     $count = mysqli_num_rows($query); 
//Replace table_name with your table name and `thing_to_search` with the column you want to search 
    if($count == "0" || $q == ""){ 
     $s[] = "No Country found!"; 
    }else{ 
       while($row = mysqli_fetch_array($query)){ 
     $s[] = $row['userCountry']; // Replace column_to_display with the column you want the results from 
       } 
     } 
for($x = 0; $x < $count; $x++) { 
      echo $s[$x]; 
      echo "<br>"; 
} 
+0

おそらく、この回答が作成する目立つSQLインジェクションの脆弱性に気付く価値があります... – David

+0

これはあなたが男です。今、SQLインジェクションから自分自身を守る方法は? – Peslis

+0

準備されたステートメントを使用するチュートリアルはたくさんあります。 –

-1

をあなたのPHPファイルを変更intvalに変換します。 MySQLサーバーが起動していて、すべての変数が正しく設定されていることを確認してください。

関連する問題