0

我有一个搜索表单,我有 4 个表单域

  1. 财产
  2. 最低价格
  3. 最高价格
  4. 地点

此处属性字段仅是强制性的,其余 3 个字段是可选的,现在我将告诉我的要求用户仅选择属性意味着我想显示详细信息(从 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'];
}
4

1 回答 1

0

我处理了你的代码,它有致命的错误。我测试并纠正了错误

试试下面的代码,将文件命名为select_buy_properties.php 并简单地将数据库配置设置如主机、用户、密码变量$host、$user、$password放在代码中。数据库名可以在命令mysql_select_db(' DatabaseName ', $conn)中 改变,表名

add_projects属性位置

<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_projects。

于 2016-12-01T11:48:51.203 回答