如何根据用户选择从数据库中检索数据

时间:2012-01-07 06:23:52

标签: php mysql

其实我在我的数据库中执行搜索,有两个表,租金和销售。当用户在单选按钮和其他参数的帮助下选择出租参数时,应该搜索租用表中的数据,如果全部匹配则返回结果。和第二个表相同。

但在执行我的查询后,我收到错误。请帮帮我。 我在这里发布了所有代码。

用户选择页面。

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>real estate</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>
$(document).ready(function() 
{
    $("input#tblsection").val('sale_table');
    $('input:radio[name=rbtn]').attr('checked',true);
    $("#rent").click(function() {
    $("#rentselect").attr('style', 'display:block;margin-top:20px;');
    $("#saleselect").attr('style', 'display:none;');
    $("input#tblsection").val('rent_table');
    });

    $("#sale").click(function() {
      $("#saleselect").attr('style', 'display:block;margin-top:20px;');
      $("#rentselect").attr('style', 'display:none;');
      $("input#tblsection").val('sale_table');

    });

    $("#srajax").click(function(){
     var rentvalue =  $('select#rentselect').val();
     var salevalue =  $('select#saleselect').val();
     var location =  $('select#location').val();
     var area =  $('select#Area').val();
     var bedroom =  $('select#bedroom').val();

    var table  = $('input#tblsection').val();
    $.post(" testajax.php", 
            { 
                rentvalue: rentvalue,
                salevalue: salevalue,
                location: location,
                area: area,
                bedroom: bedroom,
                table: table
            }, 
        function(data) {
                alert("Data Loaded: "+data);
         }); 
});

});



</script>
</head>

<body>

<form name="srajax" method="post">
<input type="radio" name="rbtn" value="rent" id="rent" /> <b>Rent</b>
<input type="radio" name="rbtn" value="sale" id="sale" /> <b>Sale</b>
<input type="hidden" name="tablenmae" id="tblsection" value="tablesale">

<br>
<select id="rentselect" style='display:none;'>
    <option value="">----Select Budget For Rent----</option>
    <option value="5000">5000</option>
    <option value="5000_10000">5000 to 10000</option>
    <option value="11000_20000">11000 to 20000</option>

    <option value="above_20000">Above 20000</option>
</select>


<select id="saleselect" style='margin-top:20px;margin-bottom:20px;'>
    <option value="">----Select Budget For Sale----</option>
    <option value="5000">100000</option>
    <option value="5000_10000">500000</option>
    <option value="11000_20000">1000000</option>

    <option value="above_20000">2500000</option>
</select>
<br>

<select id="location" name="Location">
<option>----Select Location---</option> <option value="All_Location">All Location</option>
<option value="Central_Mumbai">Central Mumbai</option>
<option value="Mumbai_Harbour">Mumbai Harbour</option>
<option value="Mumbai_Navi">Mumbai Navi</option>

<option value="Mumbai_South">Mumbai South</option>
<option value="Mumbai_Thane">Mumbai Thane</option> </select>

<br>
<br>
<select id="Area" name="Area">
<option value="">-----Select Area---</option>
<option value="All Area">All Area</option>
<option value="Bhandup">Bhandup</option>
<option value="Chembur">Chembur</option>

<option value="Kurla">Kurla</option>
<option value="Mulund">Mulund</option>

<option value="All Area">All Area</option>
<option value="Byculla">Byculla</option>
<option value="Chembur">Chembur</option>
<option value="Govandi">Govandi</option>
<option value="Sewri">Sewri</option>
<option value="Wadala">Wadala</option>

<option value="All Area">All Area</option>
<option value="Airoli">Airoli</option>
<option value="Belapur">Belapur</option>
<option value="Ghansoli">Ghansoli</option>
<option value="Mahape">Mahape</option>
<option value="Nerul">Nerula</option>

<option value="All Area">All Area</option>
<option value="Churchgate">Churchgate</option>

<option value="CST">CST</option>
<option value="Dadar">Dadar</option>
<option value="Fort">Fort</option>

<option value="All Area">All Area</option>
<option value="Brindavan">Brindavan</option>
<option value="Kalothe">Kalothe</option>
<option value="Kapur">Kapur</option>
<option value="Kalwa">Kalwa</option>

<option value="Kopat">Kopat</option> </select><br /><br>
<select id="bedroom" name="bedroom">
<option>---Select Bedroom---</option>
<option value="1BHK">1 BHK</option>
<option value="2BHK">2 BHK</option>
<option value="3BHK">3 BHK</option>
<option value="4BHK">3 BHK</option>
</select>


<br><br><br>
<input type="button" name="search" id="srajax" value="Search"/>
</form>
</body>
</html>

我的搜索页

<?php 

$host="localhost"; // Host name 

$db_name="netelmbn_realestate"; // Database name 
$tbl1_name="rent_table"; // Table1 name  
$tbl2_name="sale_table"; //Table2 name 
mysql_connect("localhost","netelmbn","password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB"); 



echo "<pre>";
print_r($_POST);

if($_POST['table'] == 'rent_table')
{
  $result=mysql_query("select * from rent_table where location='$location' AND area='$area' AND bedroom='$bedroom' AND $budget='budget' ");
echo "<table cellpadding='20'>";

                echo '<tr>'; 


 while ($row = mysql_fetch_assoc($result)) { 
echo "<td width='300'><strong>Budget:</strong> ".$row['budget']."<br><strong>Location</strong>: ".$row['location']."<br><strong>Area:</strong> ".$row['area']."<br><strong>BHK:</strong> ".$row['bhk']."<br></td>";
}
echo "</tr></table>";
}

if($_POST['table'] == 'sale_table')
{
   $result=mysql_query("select * from sale_table where location='$location' AND area='$area' AND bedroom='$bedroom' AND $budget='budget' ");
echo "<table cellpadding='20'>";

                echo '<tr>'; 


 while ($row = mysql_fetch_assoc($result)) { 
echo "<td width='300'><strong>Budget:</strong> ".$row['budget']."<br><strong>Location</strong>: ".$row['location']."<br><strong>Area:</strong> ".$row['area']."<br><strong>BHK:</strong> ".$row['bhk']."<br></td>";
}
echo "</tr></table>";
}
while ($row = mysql_fetch_assoc($result)) { 
echo "<td width='300'><strong>Budget:</strong> ".$row['budget']."<br><strong>Location</strong>: ".$row['location']."<br><strong>Area:</strong> ".$row['area']."<br><strong>BHK:</strong> ".$row['bhk']."<br></td>";
}
echo "</tr></table>";
exit;

我有rent_table

CREATE TABLE `rent_table` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `location` varchar(40) NOT NULL,
  `area` varchar(40) NOT NULL,
  `bedroom` varchar(20) NOT NULL,
  `budget` varchar(20) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=58 ;

相同的sale_table

请在我出错的地方帮助我。这是错误图像我收到此错误:

error http://netelity.com/realestate/error.png

3 个答案:

答案 0 :(得分:1)

在任何和所有数据库查询之后,检查查询是否实际成功并使用适当的调试功能查找原因。不要只是假设它有效。

$result = mysql_query(...);
if (!$result) {
    echo mysql_error();
    die;
}

答案 1 :(得分:0)

好的,你去吧:

1)在if($_POST['table'] == 'rent_table')

之前添加此内容
$location = $_POST['location'];
$area = $_POST['area'];
$bedroom = $_POST['bedroom'];
$budget = $_POST['budget'];

2)您的查询有误:AND $budget='budget'应为AND budget='$budget'

答案 2 :(得分:0)

该错误表示查询中存在错误,因此MySQL结果集无效。该错误似乎在“$ budget ='budget'”中,$budget应该用引号括起来。试试这些版本:

Query1: $result=mysql_query("select * from rent_table where location='$location' AND area='$area' AND bedroom='$bedroom' AND '$budget'='budget' ");

Query2: $result=mysql_query("select * from sale_table where location='$location' AND area='$area' AND bedroom='$bedroom' AND '$budget'='budget' ");

希望它有所帮助!