twinytwo Posted December 9, 2011 Share Posted December 9, 2011 Hey guys... since im new enough to php, i was wondering if someone could talk me through this one. i have three radio buttons.. allowing the user to search by age, area and skill level.. age and area have a text box and skill level has a drop down box. If the user was to select one of the radio buttons and enter/select a value.. how would the if statement work to search the db? I know how to open a connection, but not exactly sure how to structure this one. Quote Link to comment https://forums.phpfreaks.com/topic/252804-radio-buttiontext-box-mysql-search/ Share on other sites More sharing options...
ialsoagree Posted December 9, 2011 Share Posted December 9, 2011 The coding of this is going to depend on your HTML form variables and the databse you're using and how it is designed. Assuming you use "search_by" as the name of the radio buttons, and their values correspond to age, area, skill_level, and the input box/select box is named "search" and you use these 3 names for the table column, and you're using a MySQL database: // Connect to mysql database and assign connection to $db $search_by = $_POST['search_by']; $search = $_POST['search']; if ($search_by != 'age' && $search_by != 'area' && $search_by != 'skill_level') { die('Invalid search type.'); } if ($search_by == 'age') { $search = (int)$search; // I'm assuming your database age column is an int } else { $search = '\''.mysql_real_escape_string(trim($search)).'\''; } $query = "SELECT * FROM table_name WHERE $search_by = $search"; $query_result = mysql_query($query, $db); if ($query_result === FALSE) { die(mysql_error()); } Quote Link to comment https://forums.phpfreaks.com/topic/252804-radio-buttiontext-box-mysql-search/#findComment-1296202 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.