silverglade Posted September 13, 2011 Share Posted September 13, 2011 Hi, I was wondering if anyone knows how I might do a search in a database from a form using checkboxes, where the code is like this below please? I want to do a database query like this, if firstname is checked, and gender is checked. I want to find someone in the database with that gender and first name only. If all of them are checked, find all matches to that criteria. I think this might be a lot harder than I describe, but that is what I need to do. like this mysql_query("SELECT users WHERE gender LIKE '%gender%, OR firstname LIKE....." Search the database by: <form action="search.php" method="post"> <p> <input type="checkbox" name="firstname2" size="20" id="firstname2" value="firstname2" /> First Name<br/> <input type="checkbox" name="lastname2" size="20" id="lastname2" value="lastname2" /> Last Name<br/> <input type="checkbox" name="dob2" size="20" id="dob2" value="dob2" /> Date of Birth<br/> <input type="checkbox" name="gender2" size="20" id="gender2" value="gender2" /> Gender<br/> <input type="submit" name="search" value="Search" /> <input type="reset" name="reset" value="reset" /> </form> Quote Link to comment https://forums.phpfreaks.com/topic/247016-search-using-checkboxes/ Share on other sites More sharing options...
Pandemikk Posted September 13, 2011 Share Posted September 13, 2011 Untested. I believe this is what you're looking for? <?php if (empty($firstname) AND empty($lastname) AND empty($dob) AND empty($gender)) { die('You must check one of the following boxes'); } $sql_where_clause = ''; if (!empty($firstname)) { $sql_where_clause .= 'firstname LIKE ' . $firstname . ' AND'; } if (!empty($lastname)) { $sql_where_clause .= 'lastname LIKE ' . $lastname . ' AND'; } if (!empty($dob)) { // I'm assuming date of birth is a string here $sql_where_clause .= 'dob LIKE ' . $dob . ' AND'; } if (!empty($gender)) { // I'm assuming gender is a string here (should be enum really) $sql_where_clause .= 'gender LIKE ' . $gender . ' AND'; } $sql_where_clause = mysql_real_escape_string(rtrim($sql_where_clause, ' AND')); $query = "SELECT users WHERE $sql_where_clause"; mysql_query($query); ?> Quote Link to comment https://forums.phpfreaks.com/topic/247016-search-using-checkboxes/#findComment-1268589 Share on other sites More sharing options...
silverglade Posted September 13, 2011 Author Share Posted September 13, 2011 Pandemikk LOL that is awesome! I am not that smart, but I think I understand it as "if this is checked search, " and then, "if this is checked, search the first and this one". awesome art. it's art. thank you for typing that I really appreciate it. Have a great night/day!! :( Quote Link to comment https://forums.phpfreaks.com/topic/247016-search-using-checkboxes/#findComment-1268591 Share on other sites More sharing options...
Pandemikk Posted September 13, 2011 Share Posted September 13, 2011 It is somewhat of an art. Mostly logic and problem-solving in a language much stricter than any spoken. And anyone who dives into the coding world surely must be at least relatively smart. Be sure to mark this thread as solved. Quote Link to comment https://forums.phpfreaks.com/topic/247016-search-using-checkboxes/#findComment-1268592 Share on other sites More sharing options...
silverglade Posted September 13, 2011 Author Share Posted September 13, 2011 thanks so much. I marked it solved. Quote Link to comment https://forums.phpfreaks.com/topic/247016-search-using-checkboxes/#findComment-1268593 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.