2017-02-20 7 views
-1

私のページを更新せずにmySQLデータベースからデータを取得しようとしています。ユーザーからデータを取得するには、同じデータベースのlivesearchも使用しています。ボタンを押すと、ユーザーが入力したデータベースの行のデータが表示されます。私の問題は、PHPで、私は、エンコードされた結果を出力するために、ボタンを押すと、 "null"と私はデータベースから値を取得している理由をなぜ私は見ることができませんです。自動完成でmysqlからデータを取得

Anが早くあなたの助けをありがとう:)

をここでは私のコードです:

HTML:

<h1>LIVE SEARCH WITH AJAX TEST</h1> 
<div class="ui-widget"> 
    <label for="recherche">Recherche: </label> 
    <input id="recherche" name="recherche"> 
    <button name="button" id="button">OK</button> 
</div> 
<br> 
<div id="namedata"></div> 

はJQuery:

$(document).ready(function(){ 
    $('#button').on('click', function(){ 
     var recherche = $('#recherche').val(); 
     if ($.trim(recherche) != ''){ 
      $.post('getvalue.php', {recherche: recherche}, function(data){ 
       $('#namedata').text(data); 
      }); 
     } 
    }); 
}); 

PHP:

<?php 

if (isset($_POST['recherche']) === true && empty($_POST['recherche']) === false) { 
    require 'connect.php'; 
    $query = mysql_query("SELECT capacityingo FROM smartphone WHERE name ='" . mysql_real_escape_string(trim($_POST['recherche'])) . "' "); 
    $row = mysql_fetch_array($query); 
    echo json_encode($row); 
} 
+2

** **推奨されていない**安全でない** mysql_ *関数を使用しないでください。 PHP 5.5以降(2013年)は廃止され、PHP 7では完全に削除されました。代わりにMySQLiまたはPDOを使用してください。 –

+0

また、結果をPHPにダンプし、コンソールログにレスポンス(データ)を記録して、どのように見えるかを確認する必要があります。あなたは現在、文字列のようにレスポンスを扱っています。 –

答えて

0

私はPHP7.1を持っているので、私はバグがあった場所を参照するためにそれをテストすることができませんでした、が、エラーは、PHPスクリプトにあったと考えています。 このコードがあなたに役立つことを願っています。私はあなたのコードがそれを完全に欠いていたので、エラー処理を追加しました。

<?php 

if (isset($_POST['recherche'])) 
{ 
    if (empty($_POST['recherche'])) 
    { 
     exit(); 
    } 

    //sanitize string entering db 
    $recherche = filter_input(INPUT_POST, 'recherche', FILTER_SANITIZE_STRING); 
    $recherche = filter_var($recherche, FILTER_SANITIZE_SPECIAL_CHARS); 

    //mysqli connection 
    $mysqli = new mysqli('localhost', 'root', '', 'test'); 

    //statement with FALSE return error handling 
    if (!$stmt = $mysqli->prepare("SELECT capacityingo FROM smartphone WHERE name = ?")) 
    { 
     //throw error 
     // trigger_error('The statement could not be prepared', E_USER_ERROR); 
     //or (to be readable by javascript) 
     $error = array 
      (
       'error' => 'The statement could not be prepared' 
      ); 

     echo json_encode($error); 
     exit(); 
    } 

    $stmt->bind_param('s', $recherche); 

    //execute with error handling 
    if (!$stmt->execute()) 
    { 
     //throw error 
     // trigger_error('The statement could not be executed', E_USER_ERROR); 
     //or (to be readable by javascript) 
     $error = array 
      (
       'error' => 'The statement could not be executed' 
      ); 

     echo json_encode($error); 
     exit(); 
    } 

    //make statement output to result 
    $res = $stmt->get_result(); 
    $data = array(); 
    while ($row = $res->fetch_assoc()) 
    { 
     //add each result to an array 
     $data[] = $row; 
     //if this does not work try 
     // array_push ($data, $row); 
    } 
    //print the array encoded in JSON 
    echo json_encode($data); 
} 

UPDATE あなたはオートコンプリートの検索ボックスを作りたい場合、あなたはそれを更新し、検索ボックスに、毎回ユーザーが何かとして「オートコンプリート」の部分を考慮する必要があります。あなたのJSでこのような何か:あなたの文で=演算子を使うべきではありません検索システムについても

$(document).ready(function(){ 
    $('#recherche').on('input', function(){ /* in place of your button i put your input and checked for it's update - and don't forget to make a copy of this for your search button too, I don't wanna make this update too long */ 
     var recherche = $('#recherche').val(); 
     if ($.trim(recherche) != ''){ 
      $.post('search.php', {'recherche': recherche}, function(data){ 
       $('#namedata').text(data); 
      }); 
     } 
    }); 
}); 

。あなたはSQLLIKE演算子を使用して、このようにPHPで検索クエリを置く必要があります。

$recherche = '%'. $recherche .'%'; 

SQL文の上からすべての消毒およびエスケープ文字の後。

関連する問題