jeff5656 Posted September 13, 2009 Share Posted September 13, 2009 Let's say I want to select a record based on what the user put in the form: $lname = mysql_real_escape_string($_POST['lname']); $mrn_pre = $_POST['mrn']; $mrn = ereg_replace("[^A-Za-z0-9]", "", $mrn_pre); $resident = mysql_real_escape_string($_POST['resident']); include "connectdb"; $query = "SELECT * FROM active consults WHERE patient_name = '$lname' AND mrn='$mrn' AND resident ='$resident'"; $results = mysql_query ($query) or die (mysql_error()); But what if the user left some fields blank. How would I search and ignore the blank fields (otherwise it will search WHERE mrn = " " and no record will get selected). I could probably do this with a very complicated if else type of thing but was wondering if there is an easier (and more elegant) way to exclude blank POST variables in the query. Quote Link to comment https://forums.phpfreaks.com/topic/174037-exclude-a-posted-term-if-it-is-blank/ Share on other sites More sharing options...
mikesta707 Posted September 13, 2009 Share Posted September 13, 2009 $query = "SELECT * FROM active consults WHERE patient_name = '$lname'"; $query .= (!empty(trim($mrn)) ? "AND mrn='$mrn' AND resident ='$resident'" : "AND resident ='$resident'"; $results = mysql_query($query); I added the trim in there so if there were just blanks, IE " " would become "". Hope that helps Quote Link to comment https://forums.phpfreaks.com/topic/174037-exclude-a-posted-term-if-it-is-blank/#findComment-917418 Share on other sites More sharing options...
jeff5656 Posted September 13, 2009 Author Share Posted September 13, 2009 Thank you. But what does the ? signify after (trim($mrn))? Is that a type of if statement? Quote Link to comment https://forums.phpfreaks.com/topic/174037-exclude-a-posted-term-if-it-is-blank/#findComment-917428 Share on other sites More sharing options...
jeff5656 Posted September 13, 2009 Author Share Posted September 13, 2009 Also do I need to put trim in front of each variable? I saw you just put it in front of mrn. Addendum: I get this error: Fatal error: Can't use function return value in write context in C:\wamp\www\consults\dosearch.php on line 12 line 12 is the one with the trim statement. Quote Link to comment https://forums.phpfreaks.com/topic/174037-exclude-a-posted-term-if-it-is-blank/#findComment-917435 Share on other sites More sharing options...
mikesta707 Posted September 13, 2009 Share Posted September 13, 2009 Thank you. But what does the ? signify after (trim($mrn))? Is that a type of if statement? that is called a ternary operator. check out This page on the manual to find out more. Its basically a mini if statement. its probably a good idea to put a trim in front of each statement so you don't have extra whitespace that might cause problems. but as far as needing it, unless you alway want to check that those are not empty and that they may have whitespace, i suppose there isn't a need for it. however I would suggest trimming those values anyways, Quote Link to comment https://forums.phpfreaks.com/topic/174037-exclude-a-posted-term-if-it-is-blank/#findComment-917437 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.