Skip to main content
June 22, 2010
Answered

CF search function

  • June 22, 2010
  • 1 reply
  • 1464 views

Trying to create a user defined search function and keep getting errors. I wanted a user to type in a search field and the query would return results that match and are also similar to their search criteria. here is the code:

<!--- Get bulbs for Select List --->
<cfquery name="rsBulbs" datasource="rlBulbs">
SELECT item
FROM rlbBulbs
ORDER BY item ASC
</cfquery>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Member Search</title>
<link href="rlb.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="wrapper">
  <cfinclude template="header.cfm">
  <div id="main">
    <div id="sidebar"><cfinclude template="sidebar.cfm"></div>
    <div id="content">
      <h2>Search for a Member</h2>
      <form action="results.cfm" method="post">
        <table width="500" border="0" cellpadding="2" cellspacing="0">
          <tr>
            <td colspan="2" valign="top"><p>Please enter data in one or more of the
              fields below and <br />
              click the Search button.<br />
               </p></td>
          </tr>
          <tr>
            <th>Item Number:</th>
            <td><input type="text" name="item" size="50" /></td>
          </tr>
          <tr>
            <th>Base Type:</th>
            <td><input type="text" name="base" size="50" /></td>
          </tr>
          <tr>
            <th>Glass Type:</th>
            <td><input type="text" name="glass" size="50" /></td>
          </tr>
          <tr>
            <td align="center" colspan="2"><input type="submit" value="Search" />
              <input type="reset" value="Reset" name="reset" />          </td>
          </tr>
        </table>
      </form>
        </div>
  </div>
  <cfinclude template="footer.cfm">
</div>
</body>
</html>

here is the results page:


<cfparam name="FORM.item" default="" type="String">
<cfparam name="FORM.base" default="" type="String">
<cfparam name="FORM.glass" default="" type="String">
<!--- Find the Member Record --->
<cfquery name="rsSearch" datasource="rlBulbs">
SELECT      id,
   item,
   base,
   glass, 
FROM rlbbulbs
WHERE 0 = 0
<cfif item IS NOT "">
AND item LIKE '#item#%'
</cfif>
<cfif base IS NOT "">
AND base LIKE '#base#%'
</cfif>
<cfif glass IS NOT "">
AND glass LIKE '#glass#%'
</cfif>
ORDER BY item
</cfquery>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Members Search Results</title>
<link href="members.css" rel="stylesheet" type="text/css" />
</head>
<body>
<cfinclude template="header-members.cfm">
<h2>Search Results</h2>
<table border="1" cellpadding="4" cellspacing="0">
  <tr>
    <th>Item</th>
    <th>Base</th>
    <th>Glass</th>
  </tr>
  <cfoutput query="rsSearch">
    <tr>
      <td><a href="details.cfm?id=#rsSearch.id#">#item#</a> </td>
      <td><a href="details.cfm?id=#rsSearch.id#">#base#</a> </td>
      <td><a href="details.cfm?id=#rsSearch.id#">#glass#</a> </td>
    </tr>
  </cfoutput>
</table>
<cfinclude template="footer-members.cfm">
</body>
</html>

Can anyone spot whats wrong? I get an error that says something about a SQL syntax error and its point to line 19: "and glass is LIKE '#glass#%'

any help would be greatly appreciated.

    This topic has been closed for replies.
    Correct answer ilssac

    lol so what else could have changed when I took out one comma to cause this...I mean the search page loads, so I would assume the site is not under maintenance and of course this on my localhost. lol but somewhere between typing in a search in the search page and then passing those parameters to the result page something goes wrong.


    Temporarily replace your results.cfm file with a file that simply outputs a string to make sure you are getting where you think you are getting.

    If that works, restore the results page and start working down the file to see where it breaks.  Generous use of <cfdump...></cfabort> lines can allow you to step through your code until it breaks.

    It's possilble that that "Down for Maintance" is some type of default page to be displayed when something else is not available. or working correctly.

    1 reply

    June 22, 2010

    hmm ok well i inserted raw html...lol not sure how to post code here then! how do you post code??

    ilssac
    Inspiring
    June 22, 2010

    teedoffnewbie wrote:

    hmm ok well i inserted raw html...lol not sure how to post code here then! how do you post code??

    Try the "Insert" function looks like the two blue greater then signs ">>" next to the face.

    Also include the processed SQL code generated.  You can get this from the debug output AND|OR a dump of the <cfquery ... result="debugInfo"> <cfdump var="#debugInfo#">

    June 22, 2010

    ianskinner wrote:

    teedoffnewbie wrote:

    hmm ok well i inserted raw html...lol not sure how to post code here then! how do you post code??

    Try the "Insert" function looks like the two blue greater then signs ">>" next to the face.

    Also include the processed SQL code generated.  You can get this from the debug output AND|OR a dump of the <cfquery ... result="debugInfo"> <cfdump var="#debugInfo#">

    well that's what I thought I did...clicked the >> button then clicked insert raw html but when I posted my question, both the page codes disappeared.

    Error Executing Database Query.

    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM rlbbulbs WHERE 0 = 0 AND item LIKE '75%' ORDER BY item' at line 5
    The error occurred in C:\ColdFusion8\wwwroot\rlb\results.cfm: line 19
    17 : </cfif>
    18 : <cfif glass IS NOT "">
    19 : AND glass LIKE '#glass#%'
    20 : </cfif>
    21 : ORDER BY item
    

    SQLSTATE  42000
    SQL   SELECT id, item, base, glass, FROM rlbbulbs WHERE 0 = 0 AND item LIKE '75%' ORDER BY item
    VENDORERRORCODE  1064
    DATASOURCE  rlBulbs
    Resources:

    Browser  Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2)
    Remote Address  0:0:0:0:0:0:0:1
    Referrer  http://localhost:8500/rlb/search.cfm
    Date/Time  22-Jun-10 01:03 PM
    Stack Trace (click to expand)


    That is the debug report from my browser