Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sql statement fails with empty field from form (PHP)

Status
Not open for further replies.

rmpuk

Technical User
Nov 19, 2001
32
GB
I have a form which passes fields $formitem, $keyword, and $type to the following PHP script. The problem is that when the user leaves a field blank, the script interprets it as "all fields". eg if $formitem=015 and $keyword=dog, it will show BOTH item 015 and items with DOG in the text. Similarly, if $keyword is left blank, it returns all records. Snippet of code is below.

Is the problem with the SQL statement, or do I need to use PHP to deal with "empty" variables? Or something completely different?! What I think I need to do is tell the script that if a variable is blank, then ignore it.

Any ideas gratefully appreciated ;) Thanks.

-----------------

Code:
$result = mysql_query("SELECT * FROM tab1 WHERE
((catno='%$formitem%'
OR (text1 LIKE '%$keyword%' OR text2 LIKE '%$keyword%'))
AND section LIKE '%$type%')
AND sold='Unsold'
ORDER BY $order",$db);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top