2016-04-26 119 views
1

我下面這個教程:
http://coderexample.com/datatable-demo-server-side-in-phpmysql-and-ajax/數據表+服務器端處理+搜索過濾

,這裏是演示:
http://coderexample.com/demo/datatable-demo-server-side-in-phpmysql-and-ajax/

如果我在搜索輸入搜索ou我得到No matching records found但什麼我想返回的是至少這一行Airi Satou

this是我必須改變的代碼,我認爲,因爲我必須執行搜索服務器端。

<?php 
/* Database connection start */ 
$servername = "localhost"; 
$username = "root"; 
$password = "Password1"; 
$dbname = "test"; 

$conn = mysqli_connect($servername, $username, $password, $dbname) or die("Connection failed: " . mysqli_connect_error()); 

/* Database connection end */ 


// storing request (ie, get/post) global array to a variable 
$requestData= $_REQUEST; 


$columns = array( 
// datatable column index => database column name 
    0 =>'employee_name', 
    1 => 'employee_salary', 
    2=> 'employee_age' 
); 

// getting total number records without any search 
$sql = "SELECT employee_name, employee_salary, employee_age "; 
$sql.=" FROM employee"; 
$query=mysqli_query($conn, $sql) or die("employee-grid-data.php: get employees"); 
$totalData = mysqli_num_rows($query); 
$totalFiltered = $totalData; // when there is no search parameter then total number rows = total number filtered rows. 


if(!empty($requestData['search']['value'])) { 
    // if there is a search parameter 
    $sql = "SELECT employee_name, employee_salary, employee_age "; 
    $sql.=" FROM employee"; 
    $sql.=" WHERE employee_name LIKE '".$requestData['search']['value']."%' "; // $requestData['search']['value'] contains search parameter 
    $sql.=" OR employee_salary LIKE '".$requestData['search']['value']."%' "; 
    $sql.=" OR employee_age LIKE '".$requestData['search']['value']."%' "; 
    $query=mysqli_query($conn, $sql) or die("employee-grid-data.php: get employees"); 
    $totalFiltered = mysqli_num_rows($query); // when there is a search parameter then we have to modify total number filtered rows as per search result without limit in the query 

    $sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]." ".$requestData['order'][0]['dir']." LIMIT ".$requestData['start']." ,".$requestData['length']." "; // $requestData['order'][0]['column'] contains colmun index, $requestData['order'][0]['dir'] contains order such as asc/desc , $requestData['start'] contains start row number ,$requestData['length'] contains limit length. 
    $query=mysqli_query($conn, $sql) or die("employee-grid-data.php: get employees"); // again run query with limit 

} else {  

    $sql = "SELECT employee_name, employee_salary, employee_age "; 
    $sql.=" FROM employee"; 
    $sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]." ".$requestData['order'][0]['dir']." LIMIT ".$requestData['start']." ,".$requestData['length']." "; 
    $query=mysqli_query($conn, $sql) or die("employee-grid-data.php: get employees"); 

} 

$data = array(); 
while($row=mysqli_fetch_array($query)) { // preparing an array 
    $nestedData=array(); 

    $nestedData[] = $row["employee_name"]; 
    $nestedData[] = $row["employee_salary"]; 
    $nestedData[] = $row["employee_age"]; 

    $data[] = $nestedData; 
} 



$json_data = array(
      "draw"   => intval($requestData['draw']), // for every request/draw by clientside , they send a number as a parameter, when they recieve a response/data they first check the draw number, so we are sending same number in draw. 
      "recordsTotal" => intval($totalData), // total number of records 
      "recordsFiltered" => intval($totalFiltered), // total number of records after searching, if there is no searching then totalFiltered = totalData 
      "data"   => $data // total data array 
      ); 

echo json_encode($json_data); // send data as json format 

?> 

我有權說這是我必須改變的代碼嗎?
如果是這樣,任何人都可以建議我必須做什麼?
我明白這是很多問,但會很感激的指導!

回答

2
$sql.=" WHERE employee_name LIKE '".$requestData['search']['value']."%' "; 

將匹配,則搜索詞什麼(由於通配符%)

,只要你想在名稱中間匹配搜索詞,你需要在乞討,以及添加外卡:

$sql.=" WHERE employee_name LIKE '%".$requestData['search']['value']."%' "; 

注意這將禁用employee_name索引的使用可能會或可能不會是你的問題。

這不是最好的搜索方法,你不應該檢查全部三個字段,而是要求搜索者使用哪一個。畢竟年齡和薪水可能有一些匹配的數字。

27搜索,可以匹配27歲或27000等薪水,沒有人會擁有鮑勃所以它沒有意義的年齡做搜索

+0

不要以爲索引在這種情況下很重要,但也是很好的一點。 我正在努力尋找每列,但我很難。問我問[這裏](http://stackoverflow.com/questions/36706424/datatables-connecting-to-a-db-getting-data-but-some-features-not-working)。也許你在正確的方向上指向我? 而且我必須對每列做同樣的事情,例如搜索'27'這個工資'162700'出現。 – HattrickNZ

+0

@HattrickNZ我不知道如果這個問題現在回答與否的評論 – 2016-04-26 22:57:12

1

您的查詢需要更新。

下面的SQL語句與employee_name都開始選擇「搜索字段值」:

$sql.=" WHERE employee_name LIKE '".$requestData['search']['value']."%' "; 

所以,您的查詢應該是類似下面,得到所需的輸出,因爲它檢查是否一個特定的模式存在於employee_name中。

$sql.=" WHERE employee_name LIKE '%".$requestData['search']['value']."%' "; 
+0

tks與上述dagon相同。 – HattrickNZ

1

您構建Where子句的方式,您搜索的字段必須以搜索詞開頭。將您的where子句更改爲

$sql.=" WHERE employee_name LIKE '%".$requestData['search']['value']."%' "; 

您也可以通過執行您正在執行的操作來開放SQL注入。您需要使用參數化查詢來消除此漏洞。有關信息,請參閱How can I prevent SQL injection in PHP?

+0

與上面的dagon相同。併爲SQL注入鏈接 – HattrickNZ