mysql搜索表w / php并返回结果

时间:2013-06-04 09:50:05

标签: php mysql

我试图在我的数据库中查找$ searchtext,其中$ selecteditem代表它正在搜索的columb。我在代码的最后部分出现语法错误

我的表格 -

<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>

我的php for this-

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

8 个答案:

答案 0 :(得分: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());
}

答案 1 :(得分:5)

  1. 删除第二个WHERE
  2. 删除从$columbname变量获得的列名称周围的引号。
  3. 更改

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

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

    旁注:您的代码容易受到sql注入攻击。切换到mysqli或PDO并使用预准备语句。

答案 2 :(得分: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

答案 3 :(得分:4)

请在SQL查询中仅用AND WHERE替换AND

答案 4 :(得分:3)

使用此(删除第二个):

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

答案 5 :(得分:3)

修复查询。使用此:

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

而不是:

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

(你有太多 WHERE

答案 6 :(得分: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_扩展名。使用mysqli   或PDO代替。

答案 7 :(得分:0)

更改您的查询:

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