具有多个过滤器/字段的PHP MYSQL HTML搜索

时间:2016-07-05 09:23:59

标签: php mysql search filter multiple-tables

我有一个包含5个表的MySQL数据库

 - companies
 - place
 - category
 - sub-category
 - company-sub-category
'companies' consist of 6 column:

 - company_id (**PK**)

 - company name 

 - place_id (**FK**)

 - address

 - cat_id  (**FK**)

 - company_subcat_id  (**FK**)
'place' consist of 2 column:

 - place_id (**PK**)

 - place_name  (**FK**)
'category' consist of 2 column:

 - cat_id (**PK**)

 - cat_name  (**FK**)
'sub-category' consist of 2 column:

 - sub-cat_id(**PK**)

 - sub-cat_name  (**FK**)
'company-sub-category' consist of 3 column:

 - company_subcat_id (**PK**)

 - company_id   (**FK**)

 - sub-cat_id   (**FK**)

我想做一个: -

  1. 使用MySQL中的数据库使用PHP HTML进行搜索。

  2. 搜索表单也有一个过滤系统,如下面的html所示。

  3. 我希望结果包含公司名称

  4. 我还希望将结果显示在另一个页面" search-results.html"

  5. 我对php很新,我不确定如何使搜索表单正常工作。

    
    
    <!-- Search -->
    
    <nav id="c-menu--slide-top" class="c-menu c-menu--slide-top">
      <button class="c-menu__close" style="float: right;background: transparent;font-size: 45px;">
      <strong>x</strong></button>
      <ul class="c-menu__items">
        <li class="c-menu__item">
          <form action="" target="search-results.html" method="post" id="nl-form" class="nl-form">
              I am looking for
              <select name="category" id="category">
                <option value="0" selected>category choice</option>
                <option value="1">Women</option>
                <option value="2">Men</option>
                <option value="3">Kids</option>
              </select>'s
              <br />
              <select name="sub-category" id="sub-category">
                <option value="" selected>-sub-category</option>
                <option value="1">Jackets</option>
                <option value="2">Tops</option>
                <option value="3">Bottoms</option>
                <option value="4">Accesories</option>
                <option value="5">Undergarments</option>
                <option value="6">Shoes</option>
              </select>
              <br />around 
              <select name="price_range" id="price_range">
                <option value="0" selected>price range</option>
                <option value="$">$</option>
                <option value="$$">$ $</option>
                <option value="$$$">$ $ $</option>
                <option value="$$$$">$ $ $ $</option>
              </select>
              eatery
              <br /> and would prefer
              <select name="pickup_choice" id="pickup_choice">
                <option value="0" selected>prefered chioce</option>
                <option value="1">Home Delivery</option>
                <option value="2">Self pick-up</option>
              </select>
              <br />at
               <input type="text" value="" name="place" placeholder="location" data-subline=" eg.<em>little India</em> or <em>Chinatown</em>etc"/>
              <div class="nl-submit-wrap">
                <button class="nl-submit" type="submit"  href="search-result.html" name="submit"><a href="search-result.html"> Find a store</a></button>
    
    
    
              </div>
              <div class="nl-overlay"></div>
            </form>
            <script src="js/nlform.js"></script>
         </li>
      </ul>
    </nav>
    
    <!--End of Search-->
    &#13;
    &#13;
    &#13;

    这是我使用的html文件,我没有包含CSS和JS文件,因为我认为没有必要,但是如果有人希望我展示它,告诉我,我会成为非常乐意这样做。

    非常感谢您的帮助,我想提前感谢您。

    这是我来的PHP

        <?php
    
    
            $whereClauses = array(); 
            if (! empty($_POST['category'])) $whereClauses[] ="category='".//**mysql_real_escape_string($_POST['category'])."'";
    
            if (! empty($_POST['sub_category'])) 
              $whereClauses[] ="category='".mysql_real_escape_string($_POST['sub_category'])."'"; 
            $where = ''; 
    
            if (! empty($_POST['price_range'])) 
              $whereClauses[] ="price_range='".mysql_real_escape_string($_POST['price_range'])."'"; 
            $where = ''; 
    
            if (! empty($_POST['pickup_choice'])) 
              $whereClauses[] ="pickup_choice='".mysql_real_escape_string($_POST['pickup_choice'])."'"; 
            $where = ''; 
    
            if (count($whereClauses) > 0) 
            { 
              $where = ' WHERE '.implode(' AND ',$whereClauses); 
            } 
    
            $sql = mysql_query("SELECT * FROM mytable ORDER BY id DESC $limit" .$where);//**  
            $result=mysql_query($sql);
            while ($row = mysql_fetch_assoc($result)) {
            echo $row['cuisine'];
            echo $row['Jobs'];
    
            }
            ?>
    
    <!-- end snippet -->
    

    这是&#39; search-result.html&#39; page:_

        </head>
            
    
          <body>
        <div class="container">
            <div class="well well-sm list-grid-toggle">
                <strong>Category Title</strong>
                <div class="btn-group">
                    <a href="#" id="list" class="btn btn-default btn-sm"><span class="glyphicon glyphicon-th-list">
                    </span>List</a> <a href="#" id="grid" class="btn btn-default btn-sm"><span
                        class="glyphicon glyphicon-th"></span>Grid</a>
                </div>
            </div>
            <div id="products" class="row list-group">
                <div class="item  col-xs-6 col-lg-4">
                    <div class="thumbnail comany-detail">
                        <img class="group list-group-image" src="http://placehold.it/400x250/000/fff" alt="" />
                        <div class="caption">
                            <h4 class="group inner list-group-item-heading">
                                Company Name</h4>
                            <p class="group inner list-group-item-text">
                                Product description... Lorem ipsum dolor sit amet, consectetuer adipiscing elit,
                                sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</p>
                            <div class="row">
                                <div class="col-xs-12 col-md-6">
                                    <p class="lead">
                                        $$$$</p>
                                </div>
                                <div class="col-xs-12 col-md-6 company-link-btn">
                                    <a class="btn btn-success company-link" href="company-profile.html">View Menu</a>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="item  col-xs-6 col-lg-4">
                    <div class="thumbnail company-detail">
                        <img class="group list-group-image" src="http://placehold.it/400x250/000/fff" alt="" />
                        <div class="caption">
                            <h4 class="group inner list-group-item-heading">
                                Company Name</h4>
                            <p class="group inner list-group-item-text">
                                Product description... Lorem ipsum dolor sit amet, consectetuer adipiscing elit,
                                sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</p>
                            <div class="row">
                                <div class="col-xs-12 col-md-6">
                                    <p class="lead">
                                        $$$$</p>
                                </div>
                                <div class="col-xs-12 col-md-6 company-link-btn">
                                    <a class="btn btn-success company-link" href="company-profile.html">View Menu</a>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="item  col-xs-6 col-lg-4">
                    <div class="thumbnail company-detail">
                        <img class="group list-group-image" src="http://placehold.it/400x250/000/fff" alt="" />
                        <div class="caption">
                            <h4 class="group inner list-group-item-heading">
                                Company Name</h4>
                            <p class="group inner list-group-item-text">
                                Product description... Lorem ipsum dolor sit amet, consectetuer adipiscing elit,
                                sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</p>
                            <div class="row">
                                <div class="col-xs-12 col-md-6">
                                    <p class="lead">
                                        $$$$</p>
                                </div>
                                <div class="col-xs-12 col-md-6 company-link-btn">
                                    <a class="btn btn-success company-link" href="company-profile.html">View Menu</a>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="item  col-xs-6 col-lg-4">
                    <div class="thumbnail restaurant-detail">
                        <img class="group list-group-image" src="http://placehold.it/400x250/000/fff" alt="" />
                        <div class="caption">
                            <h4 class="group inner list-group-item-heading">
                                Restaurant Name</h4>
                            <p class="group inner list-group-item-text">
                                Product description... Lorem ipsum dolor sit amet, consectetuer adipiscing elit,
                                sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</p>
                            <div class="row">
                                <div class="col-xs-12 col-md-6">
                                    <p class="lead">
                                        $$$$</p>
                                </div>
                                <div class="col-xs-12 col-md-6 restaurant-link-btn">
                                    <a class="btn btn-success restaurant-link" href="restaurant-profile.html">View Menu</a>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="item  col-xs-6 col-lg-4">
                    <div class="thumbnail restaurant-detail">
                        <img class="group list-group-image" src="http://placehold.it/400x250/000/fff" alt="" />
                        <div class="caption">
                            <h4 class="group inner list-group-item-heading">
                                Company Name</h4>
                            <p class="group inner list-group-item-text">
                                Product description... Lorem ipsum dolor sit amet, consectetuer adipiscing elit,
                                sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</p>
                            <div class="row">
                                <div class="col-xs-12 col-md-6">
                                    <p class="lead">
                                        $$$$</p>
                                </div>
                                <div class="col-xs-12 col-md-6 company-link-btn">
                                    <a class="btn btn-success company-link" href="restaurant-profile.html">View product</a>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="item  col-xs-6 col-lg-4">
                    <div class="thumbnail company-detail">
                        <img class="group list-group-image" src="http://placehold.it/400x250/000/fff" alt="" />
                        <div class="caption">
                            <h4 class="group inner list-group-item-heading">
                                Company Name</h4>
                            <p class="group inner list-group-item-text">
                                Product description... Lorem ipsum dolor sit amet, consectetuer adipiscing elit,
                                sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</p>
                            <div class="row">
                                <div class="col-xs-12 col-md-6">
                                    <p class="lead">
                                        $$$$</p>
                                </div>
                                <div class="col-xs-12 col-md-6 company-link-btn">
                                    <a class="btn btn-success company-link" href="company-profile.html">View product</a>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
        </div>
    
            <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
    
            <script src="js/search-result.js"></script>
            
            
          </body>
        </html>

1 个答案:

答案 0 :(得分:1)

更改<form action="" target="search-results.html"

更改<form action="search-results.html"

操作是要提交的页面,目标定义它是否应该在新页面或同一页面窗口中。

关于提交按钮,无需在按钮或文本中进行链接。提交按钮链接到表单,并将提交到您在表单标记中的action属性中设置的任何内容。

更改<button class="nl-submit" type="submit" href="search-result.html" name="submit"><a href="search-result.html"> Find a store</a></button>

至:<button class="nl-submit" type="submit" name="submit">Find a store</button>

这有帮助吗?

此外,将表单数据提交到HTML页面将不起作用。它只会加载HTML页面。您将需要一个PHP页面来提取表单数据,搜索数据库,然后在页面上显示它。尝试在这里学习:W3schools PHP form handling tutorial

编辑:

Stack Overflow可以帮助您解决编程问题。不为你编程。你应该真的做一些关于PHP的教程。看看W3Schools。您可能还想阅读SQL。 (W3Schools)。以及PHP和SQL的组合:W3Schools PHP SQL

您的PHP代码充满了错误,不应使用。当你尝试编写一个新的时候要注意的事项:使用mysqli而不是旧的弃用的mysql。您的数据库连接在哪里?你试过做mysql_query TWICE。这将导致错误。 如果您遇到任何问题,请随时提出。但是,请尝试自己学习一些PHP / SQL。关于此,有很多优秀而简单的教程,因为我链接了,你可以google。

祝你好运。