Jump to content

Multiple Checkbox Query Search


cro91

Recommended Posts

After a few days of searching and running across a problem what is almost like mine on these forums (cant seem to find the link now) where the person was looking to search a database using checkboxes.

 

What I am trying to do is use a list of checkboxes (http://www.animeseason.com/anime-list/categories/) exactly like this link and search my table which has the genres as columns.

 

I am new to PHP and don't really have a starting point. I have taken a bunch of examples and came up with this.

 

My HTML form with a list of all my checkboxes.

<form method="post" ACTION = "php/do_search.php">
                           <ul class="cat_col">
                               <li><input type="checkbox" name="cat_check[]" value="1"> <a href="#Action">Action</a></li>
                                <li><input type="checkbox" name="cat_check[]" value="2"> <a href="#Adventure">Adventure</a></li>
                                <li><input type="checkbox" name="cat_check[]" value="3"> <a href="#Comedy">Comedy</a></li>
                                <li><input type="checkbox" name="cat_check[]" value="4"> <a href="#Demons">Demons</a></li>
                                <li><input type="checkbox" name="cat_check[]" value="5"> <a href="#Drama">Drama</a></li>
                                <li><input type="checkbox" name="cat_check[]" value="6"> <a href="#Ecchi">Ecchi</a></li>
                                <li><input type="checkbox" name="cat_check[]" value="7"> <a href="#Fantasy">Fantasy</a></li>
                                <li><input type="checkbox" name="cat_check[]" value="8"> <a href="#Harem">Harem</a></li>
                                <li><input type="checkbox" name="cat_check[]" value="9"> <a href="#Horror">Horror</a></li>
                                <li><input type="checkbox" name="cat_check[]" value="10"> <a href="#Magic">Magic</a></li>
                                <li><input type="checkbox" name="cat_check[]" value="11"> <a href="#Martial Arts">Martial Arts</a></li>
                                <li><input type="checkbox" name="cat_check[]" value="12"> <a href="#Mecha">Mecha</a></li>
                                <li><input type="checkbox" name="cat_check[]" value="13"> <a href="#Music">Music</a></li>
                           </ul>
                            <ul class="cat_col">
                               <li><input type="checkbox" name="cat_check[]" value="14"> <a href="#Mystery">Mystery</a></li>
                                <li><input type="checkbox" name="cat_check[]" value="15"> <a href="#Ninja">Ninja</a></li>
                                <li><input type="checkbox" name="cat_check[]" value="16"> <a href="#Parody">Parody</a></li>
                                <li><input type="checkbox" name="cat_check[]" value="17"> <a href="#Psychological">Psychological</a></li>
                                <li><input type="checkbox" name="cat_check[]" value="18"> <a href="#Reverse Harem">Reverse Harem</a></li>
                                <li><input type="checkbox" name="cat_check[]" value="19"> <a href="#Romance">Romance</a></li>
                                <li><input type="checkbox" name="cat_check[]" value="20"> <a href="#Samurai">Samurai</a></li>
                                <li><input type="checkbox" name="cat_check[]" value="21"> <a href="#School Life">School Life</a></li>
                                <li><input type="checkbox" name="cat_check[]" value="22"> <a href="#Science Fiction">Science Fiction</a></li>
                                <li><input type="checkbox" name="cat_check[]" value="23"> <a href="#Seinen">Seinen</a></li>
                                <li><input type="checkbox" name="cat_check[]" value="24"> <a href="#Shoujo">Shoujo</a></li>
                                <li><input type="checkbox" name="cat_check[]" value="25"> <a href="#Shounen">Shounen</a></li>
                                <li><input type="checkbox" name="cat_check[]" value="26"> <a href="#Slapstick">Slapstick</a></li>
                           </ul>
                            <ul class="cat_col">
                               <li><input type="checkbox" name="cat_check[]" value="27"> <a href="#Slice of Life">Slice of Life</a></li>
                                <li><input type="checkbox" name="cat_check[]" value="28"> <a href="#Sports">Sports</a></li>
                                <li><input type="checkbox" name="cat_check[]" value="29"> <a href="#Supernatural">Supernatural</a></li>
                                <li><input type="checkbox" name="cat_check[]" value="30"> <a href="#Thriller">Thriller</a></li>
                                <li><input type="checkbox" name="cat_check[]" value="31"> <a href="#Tragedy">Tragedy</a></li>
                                <li><input type="checkbox" name="cat_check[]" value="32"> <a href="#Vampire">Vampire</a></li>
                           </ul>
                           <ul class="cat_col">
                                <li><input type="checkbox" name="cat_check[]" value="33"> <a href="#Movie">Movie</a></li>
                                <li><input type="checkbox" name="cat_check[]" value="34"> <a href="#OVA">OVA</a></li>
                                <li><input type="checkbox" name="cat_check[]" value="35"> <a href="#Top Rated">Top Rated</a></li>
                                <li><input type="checkbox" name="cat_check[]" value="36"> <a href="#Currently Airing">Currently Airing</a></li>
                                <li><input type="checkbox" name="cat_check[]" value="37"> <a href="#Ended">Ended</a></li>
                                <li><input type="checkbox" name="cat_check[]" value="38"> <a href="#Ongoing">Ongoing</a></li>
                           </ul>
                      <div id="submit_btn_pos">
                      <input type="submit" name="search" value="Lets Make Magic" class="submit_btn">
                            <>
                        </form>

 

 

My PHP script that right now seems to do nothing.

 

<?php
//if we got something through $_POST
if (isset($_POST['cat_check'])) {
    $con = mysql_connect("localhost","USERNAME","PASSWORD") or die("Connection error");
mysql_select_db("DATABASENAME")or die("database doesn't exist");

// sanitize user input
$cleanData = array();
foreach($_POST['cat_check'] as $_comp) {
    //$cleanData = mysql_real_escape_query($_comp);
}

// Build query
  $compStr = implode("'), ('", $cleanData);
  $sql = "SELECT anime_name WHERE $_comp='1'";
  
}
?>

 

My database has a layout of "Name of show, (every genere listed above)".

 

Thanks for any help.

Link to comment
https://forums.phpfreaks.com/topic/248144-multiple-checkbox-query-search/
Share on other sites

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.