Jump to content

Searching problem with PHP & Postgres


smti

Recommended Posts

Hi Folks,

 

I am having a problem with a search function I am writing. Here is the situation:

 

I have text box named "search_for" where users can enter the term they want to find in a particular record. Users must then select from a drop down what field in the database they would like to check the term for.

 

Users can select the following fields in the database to search:

1. ticketid

2. name

3. username

4. building

 

Searching for a particular ticket ID appears to work fine, however, searching for anything else in any one of the other database fields produces an error. For example, if I search for "Columbia" and select building from the drop down I get the error: Error in SQL query: ERROR: column "Columbia" does not exist.

 

 

Here is my search query code (and code to show result:

 


include ("../../../../includes/connection.inc.php");

// execute query
$sql = "SELECT ticketid, opendate, name, status FROM tickets where $in_what=$search_for order by ticketid asc";
$result = pg_query($dbh, $sql);
if (!$result) {
     die("Error in SQL query: " . pg_last_error());
}
  
// iterate over result set
// print each row
//while ($row = pg_fetch_array($result)) {
    $count=0;
echo '<tbody>';	
// keeps getting the next row until there are no more to get
	  while ($row = pg_fetch_array($result)) {
// Print out the contents of each row into a table
$classStr = $count++ % 2 ? '' : "class='alt'";
		echo "<tr $classStr>
		<td> $row[0]</td>
		<td> $row[1]</td>
		<td> $row[2]</td>
		<td> $row[3]</td>";

		//Grab ID and put in variable.
		$id=$row['ticketid'];
  
  

		echo "<td><a href='viewticket.php?id=$id'><img src='images/icons/view.png' alt='View Record'></a></td>
	</tr>";
}
echo '</tbody>';	

  // close connection
pg_close($dbh);

}

 

Why can I locate records using the ticketid field, but not the others? Any help would be greatly appreciated!

 

Thank You,

 

smti

Link to comment
Share on other sites

Can you add "echo $sql;" and show me the output please?  I suspect you need single quotes around the $search_for, otherwise it'll be interpreted as a column name.  You won't get the same problem for numbers because column names can't be numbers.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.