2010-09-24 102 views
0

我想使我的表可以在每列中過濾。我從DataTable.net獲得客戶端腳本。爲了做到這一點,我必須在服務器端腳本中修改這個數據表。我的表格字段是:行,型號名稱,版本,批號,序列號,ID號,批號_序列號和產品日期。datatable:使服務器端腳本過濾每列

我試圖將每列同步到這個服務器腳本,但我總是得到一個錯誤。 這下面的腳本:

sSearch: 
bEscapeRegex:true 
sSearch_0: 
bEscapeRegex_0:true 
bSearchable_0:true 
sSearch_1: 
bEscapeRegex_1:true 
bSearchable_1:true 
sSearch_2: 
bEscapeRegex_2:true 
bSearchable_2:true //data array same until sSearch_7 

編輯

這個查詢:

$sWhere = ""; 
if (postVar('sSearch') !="") 
{ 
    $sWhere = " WHERE Line LIKE '%".mysql_real_escape_string($_POST['sSearch'])."%' "; 
} 
if (postVar('sSearch_0') !="") 
{ 
    $sWhere = " AND Line LIKE '".mysql_real_escape_string($_POST['sSearch_0'])."' "; 
} 
if (postVar('sSearch_1') !="") 
{ 
    $sWhere = " AND Model_name LIKE '%".mysql_real_escape_string($_POST['sSearch_1'])."%' "; 

//直到sSearch_7

我在這個查詢得到錯誤:

error: "Error occuered during query execution:(): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AND Line LIKE '2' ORDER BY Model_name desc LIMIT 0, 10' at line 1";

回答

0

你必須鏈查詢。
,而你與每個其他條件替代$ sWhere
你可以使用串聯(.= instead of =),但讓我建議使用這個數組:

$aWhere = array(); 
if (postVar('sSearch')) { 
    $aWhere[] = "Line LIKE '%".mysql_real_escape_string($_POST['sSearch'])."%'"; 
} 
if (postVar('sSearch_0')) 
{ 
    $aWhere[] = "Line LIKE '".mysql_real_escape_string($_POST['sSearch_0'])."'"; 
} 
if (postVar('sSearch_1')) 
{ 
    $aWhere = "Model_name LIKE '%".mysql_real_escape_string($_POST['sSearch_1'])."%'"; 
} 
//and so on 
if (count($aWhere)) $where="WHERE ".implode(' AND ',$aWhere); 
$query="select * from table $where"; 
mysql_query($query) or trigger_error(mysql_error().": ".$query); 

這段代碼的最後一行將打印出來,不僅錯誤消息,但也查詢本身,這將是非常有益的

+0

謝謝我錯過了concat。 – klox 2010-09-25 02:33:29

1

WHERE * LIKE不正確。

您需要提供到位的*

有效的列名和PHP語法錯誤,是因爲:

$sWhere = " AND Line LIKE '%".mysql_real_escape_string($_POST['sSearch_0']."%' "; 

缺少一個)關閉mysql_real_escape_string function CAL

+0

我是否必須提及的所有領域? – klox 2010-09-24 09:29:02

+0

沒有..這是行不通的。你必須爲一個like子句指定一個列名。 – codaddict 2010-09-24 09:33:08

+0

okey..data可以顯示但不能過濾數據。並顯示錯誤:附近'和線樣'2'命令模型名稱desc極限0,10'在第1行「 – klox 2010-09-24 10:07:32

1

你的代碼有幾個錯誤:

  1. 我懷疑「* LIKE」是否是有效的SQL語法。您應該在此處定義單個字段名稱。如果postVar('sSearch')等於「」,您也將無法將「WHERE」添加到您的查詢中。

    if (postVar('sSearch') !="") 
    { 
         $sWhere = " WHERE * LIKE '%".mysql_real_escape_string($_POST['sSearch'])."%' "; 
    } 
    
  2. 你mysql_real_escape_string(後這裏缺少一個右paranthesis ..

    if (postVar('sSearch_0') !="") 
    { 
         $sWhere = " AND Line LIKE '%".mysql_real_escape_string($_POST['sSearch_0']."%' "; 
    } 
    
+0

我發現了另一個問題 – klox 2010-09-24 10:10:18