View Full Version : SQL Statement with options
Bengal313
07-22-2003, 05:01 PM
I know this might simple for many of you. But I am new and Trying to learn. I want to have a form that searches a database. The form should have a drop down menu that will allow to search different table fields based on the selection. The fields are "lname" and "idnum". So, my drop down menu named "type" should have those two as options. Now next to that I will have a text field called "input" which should will take in the value to search. Now, How do I write a SQL statement that will take the value from the drop down (which will tell what field to search) and the input (the string to search for) and search the table "members" Thanx
assumed $_POST['section'] and $_POST['seek']
$query = "SELECT * FROM `tablename` WHERE tablefield1='" .$_POST['section']. "' AND tablefield2 LIKE '%" .$_POST['seek']. "%'";
the LIKE call will check the entire string for matches - % means anything -eg
db_string = 'here is a string'
%string => found
%here => not found (would need a % after wards as well)
%is% => found
Bengal313
07-22-2003, 05:58 PM
Just making sure. The selection box will select the appropriate field in the table and the text box should search for the inputed string in selected field. Right!
Spookster
07-22-2003, 07:10 PM
Please read sticky threads before posting questions.
http://www.codingforums.com/showthread.php?s=&threadid=4181
Moving to MySQL forum...
oops - umm I did read the question, honest ;)
$query = "SELECT * FROM tablename WHERE " .$_POST['section']. " LIKE '%" .$_POST['seek']. "%'";
Spookster
07-22-2003, 09:13 PM
Originally posted by Ökii
oops - umm I did read the question, honest ;)
not you silly. but since you want to make a fuss you gotta read it now too.....20 times :D
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.