Link to home
Start Free TrialLog in
Avatar of grnt
grnt

asked on

Concatenate search fields

I'm building my web site using php and mysql.  I'm converting an exiting website from ASP/MS ACCESS.  I'm having a problem with a simple select statement that utilises two drop down select boxes and one single keyword box.  The results from the drop down boxes (County and License fields) work well.  The single keyword box, which searches several fileds (Company, Title, Description), produces syntax errors.  This is the code I'm trying to write on the results page:

$query_rsSearch = sprintf("SELECT * FROM projects WHERE County LIKE '%%%s%%' AND License LIKE '%%%s%%' AND Company+','+Title+','+Description+' LIKE '%%%s%%'", $varcounty__rsSearch,$varlicense__rsSearch,$varkeyword__rsSearch);

Any suggestions on how to correct this world be appreciated.
Avatar of chipple
chipple

It should be:

$query_rsSearch = sprintf("SELECT * FROM projects WHERE County LIKE '%%%s%%' AND License LIKE '%%%s%%' AND Company+','+Title+','+Description LIKE '%%%s%%'", $varcounty__rsSearch,$varlicense__rsSearch,$varkeyword__rsSearch);

Also, you should be careful of SQL injection and use mysql_escape_string on all data input by the user.

$query_rsSearch = sprintf("SELECT * FROM projects WHERE County LIKE '%%%s%%' AND License LIKE '%%%s%%' AND Company+','+Title+','+Description LIKE '%%%s%%'", mysql_escape_string($varcounty__rsSearch),mysql_escape_string($varlicense__rsSearch),mysql_escape_string($varkeyword__rsSearch));

Good luck.
Avatar of grnt

ASKER

Thanks for the input.  Good news, the syntax error message has gone but when I enter a keword in the keyword box, it produces no results.  The County and License fields still produce results but the keyword field draws a blank.  Any further suggestions?
ASKER CERTIFIED SOLUTION
Avatar of chipple
chipple

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial