search result via multiple/optional dropdown list

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
mani2102
Forum Newbie
Posts: 1
Joined: Thu May 10, 2012 3:48 pm

search result via multiple/optional dropdown list

Post by mani2102 »

below is my search form, in which i want to search any one or multiple criterias via dropdown lists/date
Image

My problem is if any field is left not selected it will give error i.e. not pass any value in the query or pass "" that is null,
what to do if i want to select all values in the list if not selected
Query is
SELECT t1.log_id, t1.student_name, t4.course_detail, t5.branch_detail, t6.source_details, t7.ref_name
FROM register_logged t1
left join course_master t4 on t4.course_id = t1.course_id
left join branch_mast t5 on t5.branch_id = t1.branch_id
left join source_master t6 on t6.source_id = t1.source_id
left join refrences_mast t7 on t7.ref_id = t1.ref_id
where t1.course_id = '$course_id' and
t1.branch_id = $branch_id and
t1.source_id = '$source_id' and
t1.ref_id = '$ref_id' and
t1.status in ('$status') and
t1.date between '$date1' & '$date2'

confused what to do???
:banghead: :banghead: :banghead:
Attachments
Clipboard02.jpg
Clipboard02.jpg (18.95 KiB) Viewed 2018 times
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: search result via multiple/optional dropdown list

Post by califdon »

You could build your query using PHP to customize the WHERE clause, based on what selections are not null or empty strings. Alternatively, you might have each select option default to the appropriate piece of the WHERE clause, such as "= 'xyz' AND " or "LIKE '%' AND ". In either case, think of it this way: in your action= script, you will retrieve your $_POST variables and can determine whether they have been selected or filled in or not; and you need to form an SQL query statement that has a WHERE clause that retrieves the appropriate data. There are several ways you can do that, as I showed above. Try one of those and if you have trouble, post back to this thread, explaining what you tried (show us the code) and what it did.
Post Reply