2013-06-04 46 views
3

我試圖讓我的搜索看起來像我的數據庫中的$ searchtext,其中$ selecteditem代表它正在搜索的columb。即時得到語法錯誤周圍的代碼的最後一部分Mysql搜索表w/php並返回結果

我的形式 -

<form name="search" id="search" method="POST" action=""> 
    <input type="text" name="searchterm" id="searchterm"> 
    <select name="selectitem"> 
     <option value="propertydescription">Property/Description</option> 
     <option value="transactiontype">Transaction type</option> 
     <option value="applicabledocument">Applicable document</option> 
     <option value="recieved">recieved</option> 
     <option value="paid">paid</option> 
    </select> 

</div></td> 
<td>&nbsp;</td> 
<td><input type="submit" name="search" value="search"></td> 

我公司招聘

if (isset($_POST['search'])) 
{ 
    $columbname = $_POST['selectitem']; 
    $searchterm = $_POST['searchterm']; 
    $query="SELECT * FROM transactions WHERE agentclient = '$agentclient' AND WHERE '$columbname' = '$searchterm'"; 
    $result = mysql_query ($query) or die(mysql_error()); 
} 
else 
+4

你不需要'和WHERE',你應該使用'和' – billyonecan

+0

你的PHP代碼是不完整的。你也打開sql注入。閱讀有關準備好的陳述。 – Leri

+0

不可避免的佈道:清理用戶輸入,不要在生產系統上使用die(mysql_error())。 – MarioP

回答

5
  1. 刪除第二WHERE
  2. 身邊,你從$columbname變量得到列名刪除引號。

變化

$query="SELECT * FROM transactions WHERE agentclient = '$agentclient' AND WHERE '$columbname' = '$searchterm'"; 

$query="SELECT * FROM transactions WHERE agentclient = '$agentclient' AND $columbname = '$searchterm'"; 

在一個側面說明:你的代碼很容易受到SQL的注射。切換到mysqli或PDO並使用預準備語句。

+0

這擺脫了錯誤,但現在搜索不起作用:/ –

3

使用PHP這個(刪除第二個地方):

$query="SELECT * FROM transactions 
WHERE agentclient = '$agentclient' 
AND '$columbname' = '$searchterm'"; 
5
if (isset($_POST['search'])) 
{ 
$columbname = $_POST['selectitem']; 
$searchterm = $_POST['searchterm']; 
$query="SELECT * FROM transactions WHERE agentclient = '$agentclient' AND $columbname = '$searchterm'"; 
$result = mysql_query ($query) or die(mysql_error()); 
} 
4

請替換AND WHERE只需要AND在你的SQL查詢中。

3

修復查詢。使用此:

$query="SELECT * FROM transactions WHERE agentclient = '$agentclient' AND '$columbname' = '$searchterm'" 

相反的:

$query="SELECT * FROM transactions WHERE agentclient = '$agentclient' AND WHERE '$columbname' = '$searchterm'" 

(你有一個太多WHERE

0

更改您的查詢:

$query = "SELECT * FROM transactions 
      WHERE agentclient = '$agentclient' 
      AND $columbname = '$searchterm'"; 
3

則應更換AND WHERE通過AND在你SELECT查詢,並$columbname

if (isset($_POST['search'])) 
{ 
$columbname = $_POST['selectitem']; 
$searchterm = $_POST['searchterm']; 
$query  = "SELECT * FROM transactions WHERE (agentclient = '$agentclient' AND $columbname = '$searchterm')"; 
$result = mysql_query ($query) or die(mysql_error()); 
} 

注意沒有必要的':不使用mysql_擴展它的棄用。改爲使用mysqliPDO

5

我更換你PHP代碼,而現在它的工作以及

if (isset($_POST['search'])) 
{ 
$columbname = $_POST['selectitem']; 
$searchterm = $_POST['searchterm']; 
$query="SELECT * FROM transactions WHERE agentclient = '$agentclient' && 'columbname' = '$searchterm'"; 
$result = mysql_query ($query) or die(mysql_error()); 
} 
else