I'm creating this search script, and the search form has a few fields (like name, address and date of birth). Only one (or more) can be entered, how do I ignore the fields that are not filled in ($_REQUEST['name'] = "") in the mysql query?? I've been trying things for hours now, and just don't know it
yes i know that.. but here's my problem. I have these fields first name last name address age someone enters age and first name, and someone else enters last name and address. I need to alter the mysql query that the empty strings are ignored.. if i do it your way, i will have to do it this way: if (!empty($name)) { mysql query for last name, address and age } if (!empty($lastname)) { mysql query for name, address and age } if (!empty($age)) { mysql query for name, last name, address } if (!empty($address)) { mysql query for name, last name and age } that's only for when there's only one blank field. then i need all combinations for two blank fields, three blank fields, etc (the real form has around 8 fields, so that would be thousands of queries..)
You just make it modular. Something like this $query = ''; if (!empty($name)) { $query .= ' AND name = \'blablabla\''; } if (!empty($address)) { $query .= ' AND address = \'blablabla\''; } mysql_query('SELECT blablabla FROM blabla WHERE 1 = 1 AND '. $query); PHP:
Is it a search query that you're doing? I'm assuming so... You could always do something like... $query_array = array(); if ( ! empty( $name ) ) { $query_array[] = 'name LIKE ' . $name; } if ( ! empty( $lastname ) ) { $query_array[] = 'lastname LIKE ' . $lastname; } ... etc ... $query_where = implode( ' AND ', $query_array ); $query = 'SELECT FROM blah WHERE ' . $query_where; PHP: Hopefully that makes sense...
Icheb: I would probably have to agree with you... we were obviously typing at the same time as there is no way I would type my own reply after seeing yours. Oh well, that's life I guess!
And here I thought you would have been able to read the thread and post a reply within 2 minutes or less. I guess I was wrong. Oh well, that's life I guess!
okay i have another problem.. how do i change $query .= ' AND address = \'blablabla\''; so that blablabla becomes a string...?? I have to remove the quotes, otherwise the output becomes $name, but when i do that, the output becomes john doe instead of 'john doe' EDIT: never mind, i have changed it... i had to change the string itself into "$name"
@Icheb: I can't quite tell from your post, but I hope you are being sarcastic... when I open all unread threads in separate tabs before even reading the first one, there are times where I don't even get to reading the thread until 15 minutes after opening it!