2016-06-11 65 views
0

我的網頁上有一個網格視圖。網格從我的數據庫中填充。我在腳本中建立了一個搜索,所以當我輸入number,nameprice時,我得到了正確的數據。Gridview搜索在添加WHERE語句後停止工作

我的SQL查詢來獲取信息如下:

$sql = "SELECT id, name, price FROM employees"; 

現在我想延長我的腳本和特定用戶的相關信息。但是,當我添加一個WHERE語句來我的SQL查詢,搜索功能停止工作

這是SQL查詢,我需要:

$sql = "SELECT id, name, price FROM employees WHERE user_id='1'"; 

我不知道什麼是錯。此查詢向我顯示正確的數據,但搜索功能停止工作。我也在phpMyAdmin中試過這個SQL查詢,並且沒有任何問題。

有人可以幫我解決這個問題。我真的不知道什麼是錯的:

這是我用來從我的數據庫中獲取信息的腳本。 SQL查詢是24行: - response.php -

<?php 
include_once("connection.php"); 

$params = $columns = $totalRecords = $data = array(); 

$params = $_REQUEST; 

$columns = array( 
0 =>'id', 
1 =>'name', 
2 => 'price', 
3 => 'user_id' 
); 

$where = $sqlTot = $sqlRec = ""; 

if(!empty($params['search']['value'])) { 
    $where .=" WHERE "; 
    $where .=" (id LIKE '".$params['search']['value']."%' ";  
    $where .=" OR name LIKE '".$params['search']['value']."%' "; 
    $where .=" OR price LIKE '".$params['search']['value']."%')"; 
} 

$sql = "SELECT id, name, price FROM employees WHERE user_id='1'"; 
$sqlTot .= $sql; 
$sqlRec .= $sql; 
if(isset($where) && $where != '') { 
    $sqlTot .= $where; 
    $sqlRec .= $where; 
} 

$sqlRec .= " ORDER BY ". $columns[$params['order'][0]['column']]." ".$params['order'][0]['dir']." LIMIT ".$params['start']." ,".$params['length']." "; 

$queryTot = mysqli_query($conn, $sqlTot) or die("database error:". mysqli_error($conn)); 

$totalRecords = mysqli_num_rows($queryTot); 

$queryRecords = mysqli_query($conn, $sqlRec) or die("error to fetch employees data"); 

while($row = mysqli_fetch_row($queryRecords)) { 
    $data[] = $row; 
} 

$json_data = array(
"draw"   => intval($params['draw']), 
"recordsTotal" => intval($totalRecords), 
"recordsFiltered" => intval($totalRecords), 
"data"   => $data 
); 

echo json_encode($json_data); 
?> 

編輯1:

現在我有:

if(!empty($params['search']['value'])) { 
    $where .=" WHERE "; 
    $where .=" (id LIKE '".$params['search']['value']."%' ";  
    $where .=" OR name LIKE '".$params['search']['value']."%' "; 
    $where .=" OR price LIKE '".$params['search']['value']."%') AND user_id='1' 

$sql = "SELECT id,name,price FROM customers"; 

當我跑,我得到沒有user_id='1'的網格視圖和user_id='1'的搜索功能。他們都需要user_id='1'

+0

http://php.net/manual/en /mysqli.error.php和var_dump查詢,你會看到這裏發生了什麼。看到你正在使用錯誤檢查,你得到了什麼錯誤? –

+0

我是否需要創建'$ sql'的'var_dump'? 'var_dump($ sql);' – John

+0

@ Fred-ii-我沒有得到任何錯誤。 – John

回答

0

如果$ params爲不爲空,你有兩個地方在SQL這是錯誤的

if(!empty($params['search']['value'])) { 
    $where .=" WHERE "; 
    $where .=" (id LIKE '".$params['search']['value']."%' ";  
    $where .=" OR name LIKE '".$params['search']['value']."%' "; 
    $where .=" OR price LIKE '".$params['search']['value']."%')"; 
} 

    $sql = "SELECT id, name, price FROM employees WHERE user_id='1'"; 

...... 
// here you assign the firts where by $sql 
$sqlTot .= $sql; 
// here you assign the second where by eval $param 
if(isset($where) && $where != '') { 
$sqlTot .= $where; 
$sqlRec .= $where; 
} 

我suggestio syou應該使用這個設置

if(!empty($params['search']['value'])) { 
    $where .=" WHERE "; 
    $where .=" (id LIKE '".$params['search']['value']."%' ";  
    $where .=" OR name LIKE '".$params['search']['value']."%' "; 
    $where .=" OR price LIKE '".$params['search']['value']."%') "; 

} 
if (!empty($where)) { 

    $where .= "AND user_id='1' "; 
} else { 
    $where .= "WHERE user_id='1' "; 
} 


$sql = "SELECT id,name,price FROM customers"; 
+0

這應該不重要。 MySQL會彌補它。 –

+0

@ Fred-ii-我經歷過並不總是這麼發生......我建議這個..在$ sql條紋傳遞查詢 – scaisEdge

+0

explecially我的猜測是他們需要擺脫'WHERE user_id ='1''完全是最可能會出錯。他們的查詢有點混亂。 –