2017-02-20 49 views
-1

我想從mySQL數據庫中獲取數據而不刷新我的頁面。爲了從用戶獲取數據,我還使用同一數據庫中的livesearch。當我按下按鈕時,我想要顯示用戶輸入的數據庫中該行的數據。我的問題是與PHP,當我按下按鈕輸出編碼結果,輸出爲「空」,我不明白爲什麼說這是因爲我從數據庫中獲取值。從自動完成從MySQL中提取數據

早期感謝您的幫助:)

這裏是我的代碼:

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

我相信錯誤是在你的PHP腳本,不過,因爲我有PHP7.1,我無法測試它,看看那裏的錯誤了。 我希望這段代碼適合你。我添加了錯誤處理,因爲你的代碼缺乏整體。

<?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上述聲明。