Jump to content

radio buttion/text box mysql search


twinytwo

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/252804-radio-buttiontext-box-mysql-search/
Share on other sites

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());
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.