我试图在我的数据库中查找$ 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> </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
答案 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)
WHERE
$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并使用预准备语句。
答案 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());
}
答案 7 :(得分:0)
更改您的查询:
$query = "SELECT * FROM transactions
WHERE agentclient = '$agentclient'
AND $columbname = '$searchterm'";