2012-01-07 81 views
-3

其實我在我的數據庫中執行搜索,有兩個表,租和銷售。當用戶通過單選按鈕等參數選擇租用參數時,應搜索租用表中的數據,並返回全部匹配的結果。第二張桌子也一樣。如何從數據庫檢索數據取決於用戶選擇

但執行我的查詢後,我得到錯誤。請幫助我。 我在這裏發佈我所有的代碼。

用戶選擇頁面。

<!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 ; 

samething爲sale_table

請幫我在哪裏,我錯了。以下是錯誤的圖像我收到此錯誤:

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

+2

你已經發布了一切,但錯誤,這可能是最重要的。 – 2012-01-07 06:34:05

+0

無論在圖像本身的錯誤是越來越 – user1083644 2012-01-07 06:52:46

回答

1

後的任何和所有的數據庫查詢,檢查如果查詢竟是成功並使用相應的調試功能找到它爲什麼不是。不要只假定它的工作。

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

我檢查了它,但它的銷售表預算的所有時間價值是5000.爲什麼這麼.. ..你可以告訴我。它說預算1附近的sysntax錯誤,但它的正確我檢查了它。 :( – user1083644 2012-01-07 07:08:27

+0

看到我的答案,也許它會解決它 – redmoon7777 2012-01-07 07:10:49

+3

@user如果它說的語法錯誤,它*不*正確。 – deceze 2012-01-07 07:13:02

0

確定在這裏你去:if($_POST['table'] == 'rent_table')

1)補充一點:

$location = $_POST['location']; 
$area = $_POST['area']; 
$bedroom = $_POST['bedroom']; 
$budget = $_POST['budget']; 

2)在查詢中有一個錯誤:AND $budget='budget'應該AND budget='$budget'

+1

調用mysql_real_escape_string真的很麻煩嗎? – 2012-01-07 07:02:04

+1

使他的代碼更好/更安全不是 – redmoon7777 2012-01-07 07:06:39

+3

@Chris,他已經將密碼發佈到了他的數據庫中,因此將數據庫打開到SQL注入不再有什麼區別;-) – Mischa 2012-01-07 07:17:13

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' "); 

希望它有幫助!

+0

作爲一個建議,請考慮清理您的數據。你可能喜歡使用mysql_real_escape_string() – Abhay 2012-01-07 07:48:27

相關問題