PHP下拉列表“全选”选项以搜索和显示MySQL数据库的结果

时间:2012-10-22 11:39:58

标签: php mysql

我对php比较新。我有多个下拉列表,用于查询数据库中的表。我想要做的是使每个下拉列表中的第一个项目在每个下拉列表中的“全选”列表项中搜索该表以查找该特定列表中的所有其他项。

Here's my HTML:
<form name="campsite_search" action="filterprop.php" method="get">
    <select name="rent_sale">
        <option value="" selected="selected">For Rent/Sale</option>
        <option value="rent">For Rent</option>
        <option value="sale">For Sale</option>
    </select>
    <select name="location">
        <option value="" selected="selected">All locations</option>
        <option value="gaborone">Gaborone</option>
        <option value="francistown">Francistown</option>  
    </select>
    <select name="housetype">
        <option value="" selected="selected">All House Types</option>
        <option value="apartment">Apartment</option>
        <option value="double">Double-storey</option>
    </select>
    <select name="bedrooms">
        <option value="" selected="selected">No. of bedrooms</option>
        <option value="1">1</option>
        <option value="2">2</option> 
        <option value="3">3</option>  
    </select>
    <select name="ensuite">
        <option value="" selected="selected">No. of bedrooms ensuite</option>
        <option value="0">0</option>
        <option value="1">1</option>
        <option value="2">3</option>
    </select>
    <select name="servantsq">
        <option value="" selected="selected">Servants Quarter</option>
        <option value="No">No</option>
        <option value="Yes">Yes</option>
    </select>
    </select>
        <select name="otherfeatures">
        <option value="" selected="selected">Other Features</option>
        <option value="pool">Swimming Pool</option>
        <option value="garage">Garage</option>
        <option value="balcony">Balcony</option>
    </select>

<input type="submit" name="submit"value="Continue" />
</form> 

我的PHP:

<?php

$con = mysql_connect ("localhost","root","");
if (!$con){
die('Could not connect:' .mysql_error());
}
mysql_select_db("my_db", $con);

            $rent_sale = $_GET['rent_sale']; 
            $location = $_GET['location']; 
            $housetype = $_GET['housetype']; 
            $bedrooms = $_GET['bedrooms']; 
            $ensuite = $_GET['ensuite']; 
            $servantsq = $_GET['servantsq']; 
            $otherfeatures = $_GET['otherfeatures']; 

$mysql_query = "SELECT * FROM property WHERE rent_sale='$rent_sale' AND location='$location' AND housetype='$housetype' AND bedrooms='$bedrooms' AND ensuite='$ensuite' AND servantsq='$servantsq' AND otherfeatures='$otherfeatures'"; 

$result = mysql_query($mysql_query) or die (mysql_error());

while($row = mysql_fetch_array($result)){
echo $row['location'];
}
mysql_close($con);
?>

到目前为止,它确实查询我的表并仅显示位置(现在可以使用)但如果我没有从每个下拉列表中选择一个项目它就不起作用,这就是为什么我想要第一个项目选择所有这一类......

先谢谢你们。

1 个答案:

答案 0 :(得分:0)

$rent_sale = if(isset($_GET['rent_sale'])){$_GET['rent_sale']}else{'*'}; 

$rent_sale = if($_GET['rent_sale'] != ""){$_GET['rent_sale']}else{'*'}; 

这两个中的一个应该可以工作,我不确定你的代码是否会返回null,如果第一个工作,如果它不是第二个应该。你显然必须为每个变量做到这一点。