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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.