2

I am trying to make my search look in my database for $searchtext where $selecteditem represents the columb it's searching. Im getting syntax errors around the last part of the code

My form-

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

My 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
4
  • 4
    You don't need AND WHERE, you should just use AND Commented Jun 4, 2013 at 9:51
  • Your php code is incomplete. Also you are open to sql injections. Read up about prepared statements. Commented Jun 4, 2013 at 9:52
  • Inevitable sermon: Sanitize user input, don't use die(mysql_error()) on production system. Commented Jun 4, 2013 at 9:56
  • do not use mysql_ extension it's deprecated. Use mysqli or PDO instead. Commented Jun 4, 2013 at 10:06

8 Answers 8

5
  1. Remove second WHERE
  2. Remove quotes around the column name that you get from $columbname variable.

Change

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

to

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

On a side note: your code is vulnerable to sql-injections. Switch to mysqli or PDO and use prepared statements.

Sign up to request clarification or add additional context in comments.

1 Comment

That gets rid of the error but now the search is not working :/
5

I have replace your PHP code, and now it's working well

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

Comments

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());
}

Comments

4

Please replace the AND WHERE by just AND in your SQL query.

Comments

3

Use this (Delete the second where):

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

Comments

3

Fix the query. Use this:

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

Instead of:

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

(you have one too many WHERE)

Comments

3

you should replace the AND WHERE by AND in you SELECT query and no need of ' in $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());
}

Note: do not use mysql_ extension it's deprecated. Use mysqli or PDO instead.

Comments

0

Change your query:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.