Jump to content

search using checkboxes


silverglade

Recommended Posts

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>

Link to comment
https://forums.phpfreaks.com/topic/247016-search-using-checkboxes/
Share on other sites

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);
?>

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!! :( :( :(8)

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.