如何搜索值强制性字段和可选字段



我有一个搜索表格,我有4个表单字段

  1. 属性
  2. 最低价格
  3. 最大价格
  4. 位置

此处属性字段仅强制性,其余3个字段是可选的,现在我会告诉我的要求用户选择属性仅表示我想显示(从db中获取匹配的记录)。支持用户选择属性,最低价格表示i是否要显示(从DB中获取匹配的记录)的详细信息,剩下的两个文件也是如此,我尝试这样尝试,但我无法获取,请参阅此处的数据库结构单击此处

$(document).ready(function(){
    $('#buy_btn').click(function(){
        if($('#buyForm').valid()){
            $.ajax({
                type:'POST',
                url :"select_buy_properties.php",
                data: $('form#buyForm').serialize(),
                success: function(data) {
                    console.log(data);
                },
                error:function(exception){
                    alert('Exeption:'+exception);
                }
            });
            return false;
        }
    });
});
<form id="buyForm" method="POST">
    <div class="col-md-11 col-sm-11 col-xs-11" >
        <div class="search1">					 
            <div class="col-md-2 col-sm-3 col-xs-3" style="padding-left:0px;padding-right: 2px;">
                <!---->
                <select class="ui search dropdown" style=" width: 100%; height: 45px;padding-left:10px;font-size: 17px !important;    font-family: inherit;" data-msg-required="Select Propery" required="" aria-required="true" name="property"> 
                    <option value="">Property</option>
                    <?php
                    $p = mysql_query("SELECT * FROM properties WHERE status !='1'");
                    while($pp = mysql_fetch_assoc($p))
                    {
                        echo '<option value="'.$pp["id"].'">'.$pp["properties"].'</option>';
                    } 
                    ?>
                </select>
                <!---->
            </div> 
            
            <div class="col-md-2 col-sm-3 col-xs-3" style="padding-left:0px;padding-right: 2px;">
                <!---->
                <select class="ui search dropdown" style=" width: 100%; height: 45px;padding-left:10px;font-size: 17px !important;    font-family: inherit;" name="min_price"> 
                    <option value="">Min Price</option>
                    <option value="500000-1000000">5-10 Lacs</option>
                    <option value="1000000-2000000">10-20 Lacs</option>
                    <option value="2000000-3000000">20-30 Lacs</option>
                    <option value="3000000-4000000">30-40 Lacs</option>
                    <option value="4000000-5000000">40-50 Lacs</option>
                    <option value="5000000-6000000">50-60 Lacs</option>
                    <option value="6000000-7000000">60-70 Lacs</option>
                    <option value="7000000-8000000">70-80 Lacs</option>
                    <option value="8000000-9000000">80-90 Lacs</option>
                    <option value="10000000-100000000">100+ Lacs</option>
                </select>
                <!---->
            </div>
            
            <div class="col-md-2 col-sm-3 col-xs-3" style="padding-left:0px;padding-right: 2px;">
                <!---->
                <select class="ui search dropdown" style=" width: 100%; height: 45px;padding-left:10px;font-size: 17px !important;    font-family: inherit;" name="max_price"> 
                    <option value="">Max Price</option>
                    <option value="1000000-2000000">10-20 Lacs</option>
                    <option value="2000000-3000000">20-30 Lacs</option>
                    <option value="3000000-4000000">30-40 Lacs</option>
                    <option value="4000000-5000000">40-50 Lacs</option>
                    <option value="5000000-6000000">50-60 Lacs</option>
                    <option value="6000000-7000000">60-70 Lacs</option>
                    <option value="7000000-8000000">70-80 Lacs</option>
                    <option value="8000000-9000000">80-90 Lacs</option>
                    <option value="10000000-100000000">100+ Lacs</option>
                </select>
                <!---->
            </div>
            
            
            <div class="col-md-4 col-sm-4 col-xs-4" style="padding-left:0px;padding-right: 2px;">
                <!---->
                <select class="ui search dropdown" style=" width: 100%; height: 45px;padding-left:10px;font-size: 17px !important;    font-family: inherit;" name="location"> 
                    <option value="">Location</option>
                    <?php
                    $l = mysql_query("SELECT * FROM location WHERE status !='1'");
                    while($ll = mysql_fetch_assoc($l))
                    {
                        echo '<option value="'.$ll["id"].'">'.$ll["location"].'</option>';
                    } 
                    ?>
                </select>
                <!---->
            </div>
            
            <div class="col-md-2 col-sm-2 col-xs-2" style="padding-left:0px;padding-right: 2px; !important;padding-right:0px !important;">
                <button type="button" class="btn btn-primary btn-lg" style="background-color: #e85700 !important;
                border-color: #e85700 !important; font-size: 14px !important;height: 44px; width:100%; padding-left:11px; " id="buy_btn">SEARCH<!--<span class="glyphicon glyphicon-search" aria-hidden="true"></span>--></button>					  
            </div>
        </div>
    </div>
</form>

select_buy_properties.php

include("admin/dbconfig.php");
$property = $_POST['property'];
$min_price = $_POST['min_price'];//5000000-1000000
$min_explode = explode("-",$min_price);
$min1= $min_explode[0] ; //5000000 5 lacs
$min2= $min_explode[1] ; //1000000 10 lacs
$max_price = $_POST['max_price'];
$max_explode = explode("-",$min_price);
$max1= $max_explode[0] ; //10,00,000 ten lacs
$max2= $max_explode[1] ; //20,00,000 twenty lacs
$location = $_POST['location'];
$query="SELECT * FROM add_projects WHERE property ='$property' AND status!=''"; 
if($min_price!='')
{
    $query.="AND ( total_price BETWEEN '600000' AND '700000')";
}
if($max_price!='')
{
    $query.="AND ( total_price BETWEEN '700000' AND '1000000')";
}
if($location!='')
{
    $query.=" AND project_location='$location'";
}
$sql=mysql_query($query);
while($rlt=mysql_fetch_assoc($sql)){
    echo $rlt['project_title'];
}

我在您的代码上工作了,它有致命的错误。我测试并纠正了错误

尝试以下代码,将文件命名为 select_buy_properties.php 简单地将数据库配置设置(例如)>主机,用户,密码变量 $ host,$ user,$ passwer 在代码中查找。数据库名称可以在命令中更改mysql_select_db(' databasename ',$ conn)和表名称

add_projects properties 位置

<html>
<head>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js">
    <script>
    $(document).ready(function(){
    $('#buy_btn').click(function(){
        alert('hai')
    if($('#buyForm').valid()){
           $.ajax({
             type:'POST',
             url :"select_buy_properties.php",
             data: $('form#buyForm').serialize(),
             success: function(data) {
             console.log(data);
           },
         error:function(exception){
         alert('Exeption:'+exception);
        }
      });
    return false;
    }
    });
    });
    </script>
    </head>
    <body>
    <?php
    ini_set('display_errors', 1);
    $host ="localhost";
    $user ="root";
    $password="root";
    $conn = mysql_connect($host,$user,$password);
    if(!$conn){
        die('Could not connect: '.mysql_error());
    }

    if (!mysql_select_db('DatabaseName', $conn)) {
        echo 'Could not select database';
        exit;
    }
    $query="";
    $property = '';
    if (isset($_POST['property']) && !empty($_POST['property'])) 
    {
        $property = $_POST['property'];
        $query
        ="SELECT * FROM add_projects WHERE status != '1' AND property = '".$property."'";
    }
    $min_price = '';
    $min1 = '';
    $min2 = '';
    if (isset($_POST['min_price']) && !empty($_POST['min_price'])) 
    {
        $min_price = $_POST['min_price'];
        $min_explode = explode("-",$min_price);
        $min1= $min_explode[0] ; //5000000 5 lacs
        $min2= $min_explode[1] ; //1000000 10 lacs
        if($min_price!='')
        {
         $query.=" AND ( total_price BETWEEN '600000' AND '700000')";
        }
    }
    $max_price = '';
    $max1 = '';
    $max2 = '';
    if (isset($_POST['max_price']) && !empty($_POST['max_price'])) 
    {
        $max_price = $_POST['max_price'];
        $max_explode = explode("-",$max_price);
        $max1= $max_explode[0] ; //10,00,000 ten lacs
        $max2= $max_explode[1] ; //20,00,000 twenty lacs
        if($max_price!='')
        {
          $query.=" AND ( total_price BETWEEN '700000' AND '1000000')";
        }
    }
    $location = '';
    if (isset($_POST['location']) && !empty($_POST['location'])) 
    {
        $location = $_POST['location'];
        if($location!='')
        {
         $query.=" AND project_location='$location'";
        }
    }

    if($query != '')
    {
        $sql=mysql_query($query);
        while($rlt = mysql_fetch_array($sql)){
            echo $rlt['project_title'];
        }
    }
    ?>
    <form id="buyForm" method="POST">
     <div class="col-md-11 col-sm-11 col-xs-11" >
       <div class="search1">                     
        <div class="col-md-2 col-sm-3 col-xs-3" style="padding-left:0px;padding-right: 2px;">
        <!---->
        <select class="ui search dropdown" style=" width: 100%; height: 45px;padding-left:10px;font-size: 17px !important;    font-family: inherit;" data-msg-required="Select Propery" required="" aria-required="true" name="property"> 
         <option value="">Property</option>
            <?php
            $p = mysql_query("SELECT * FROM properties WHERE status !='1'");
            while($pp = mysql_fetch_assoc($p))
            {
                echo '<option value="'.$pp["id"].'">'.$pp["properties"].'</option>';
            } 
            ?>
        </select>
        <!---->
        </div> 
      <div class="col-md-2 col-sm-3 col-xs-3" style="padding-left:0px;padding-right: 2px;">
        <!---->
        <select class="ui search dropdown" style=" width: 100%; height: 45px;padding-left:10px;font-size: 17px !important;    font-family: inherit;" name="min_price"> 
                <option value="">Min Price</option>
                <option value="500000-1000000">5-10 Lacs</option>
                <option value="1000000-2000000">10-20 Lacs</option>
                <option value="2000000-3000000">20-30 Lacs</option>
                <option value="3000000-4000000">30-40 Lacs</option>
                <option value="4000000-5000000">40-50 Lacs</option>
                <option value="5000000-6000000">50-60 Lacs</option>
                <option value="6000000-7000000">60-70 Lacs</option>
                <option value="7000000-8000000">70-80 Lacs</option>
                <option value="8000000-9000000">80-90 Lacs</option>
                <option value="10000000-100000000">100+ Lacs</option>
        </select>
        <!---->
        </div>
      <div class="col-md-2 col-sm-3 col-xs-3" style="padding-left:0px;padding-right: 2px;">
        <!---->
        <select class="ui search dropdown" style=" width: 100%; height: 45px;padding-left:10px;font-size: 17px !important;    font-family: inherit;" name="max_price"> 
                <option value="">Max Price</option>
                <option value="1000000-2000000">10-20 Lacs</option>
                <option value="2000000-3000000">20-30 Lacs</option>
                <option value="3000000-4000000">30-40 Lacs</option>
                <option value="4000000-5000000">40-50 Lacs</option>
                <option value="5000000-6000000">50-60 Lacs</option>
                <option value="6000000-7000000">60-70 Lacs</option>
                <option value="7000000-8000000">70-80 Lacs</option>
                <option value="8000000-9000000">80-90 Lacs</option>
                <option value="10000000-100000000">100+ Lacs</option>
        </select>
        <!---->
        </div>

        <div class="col-md-4 col-sm-4 col-xs-4" style="padding-left:0px;padding-right: 2px;">
        <!---->
        <select class="ui search dropdown" style=" width: 100%; height: 45px;padding-left:10px;font-size: 17px !important;    font-family: inherit;" name="location"> 
         <option value="">Location</option>
            <?php
            $l = mysql_query("SELECT * FROM location WHERE status !='1'");
            while($ll = mysql_fetch_assoc($l))
            {
                echo '<option value="'.$ll["id"].'">'.$ll["location"].'</option>';
            } 
            ?>
        </select>
        <!---->
        </div>
     <div class="col-md-2 col-sm-2 col-xs-2" style="padding-left:0px;padding-right: 2px; !important;padding-right:0px !important;">
      <button type="submit" class="btn btn-primary btn-lg" style="background-color: #e85700 !important;
    border-color: #e85700 !important; font-size: 14px !important;height: 44px; width:100%; padding-left:11px; " id="buy_btn">SEARCH<!--<span class="glyphicon glyphicon-search" aria-hidden="true"></span>--></button>                    
    </div>
    </div>
    </div>
    </form>

    </body>
    <?php
        mysql_close($conn);
    ?>
    </html>

我在数据库屏幕屏幕上观察了表名称add_projects1。但是您在代码中写下了表名称add_projects。请在数据库中重命名为add_project。

相关内容

  • 没有找到相关文章

最新更新