我的网站有4页:主页,销售页面,租借页面和联系我们页面。
在主页上是一个搜索表单,需要搜索他们选择的选项。这是一个物业网站,所以如果他们点击搜索按钮,他们需要被带到他们选择的销售页面或租赁页面,例如,如果他们选择出售,房子,3间卧室,2间浴室,然后无论什么金额最低价格/最高价格然后它需要转到销售页面并在他们选择的值上检索房屋。
以下是搜索表单的图片:
有人可以帮我解决这个问题吗?
以下是HTML搜索表单编码:
<form id="advanced_search" action="search.php" class="clearfix" name="advanced_search" method="post">
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
<label for="type"><b>Type</b></label>
<select>
<option value="House">House</option>
<option value="Apartment">Apartment</option>
<option value="Flat">Flat</option>
<option value="Townhouse">Townhouse</option>
<option value="Plot">Plot</option>
<option value="Farm">Farm</option>
</select>
</div>
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
<label for="status"><b>Status</b></label>
<select>
<option value="For Sale">For Sale</option>
<option value="For Rent">For Rent</option>
</select>
</div>
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
<label for="bedrooms"><b>Bedrooms</b></label>
<select>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
</div>
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
<label for="baths"><b>Bathrooms</b></label>
<select>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
</div>
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
<label for="min_price"><b>Min Price</b></label>
<select>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
</div>
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
<label for="max_price"><b>Max Price</b></label>
<select>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
</div>
<div class="clearfix"></div>
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<button name="search" type="submit" class="btn btn-success">SEARCH PROPERTY</button>
</div>
</form>
以下是我正在努力解决的PHP编码问题:
<?php
if(isset($_POST['search']))
{
$key=$_POST["search"]; //key=pattern to be searched
$con=mysqli_connect("localhost", "root", "");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result=mysqli_query($con,"SELECT * FROM properties WHERE");
while($row=mysqli_fetch_assoc($result))
{
}
}
?>
答案 0 :(得分:0)
有很多问题,首先你没有命名你的html表单标签,其次你没有在php代码中使用$ key变量来修改你的查询。您需要了解基本表单交互。我还强烈建议你研究MVC,在模板代码上方只有10行的SQL连接真的很糟糕。
答案 1 :(得分:0)
你必须改变这个: $ result = mysqli_query($ con,“SELECT * FROM properties WHERE”);
第一个问题,选项是:“最大价格,类型等......”在同一张桌子上?
你可以这样做:
$ type = $ _POST ['type'];
$ status = $ _POST ['status'];
$ minPrice = $ _POST ['minPrice'];
$ maxPrice = $ _POST ['maxPrice'];
$ result = mysqli_query($ con,“SELECT * FROM properties WHERE type ='$ type'和status ='$ status'以及'$ minPrice'和'$ maxPrice'之间的max_price”);
对于其他字段,应该工作
答案 2 :(得分:0)
在您的查询中尝试此操作:
$type = $_POST['$type'];
$status = $_POST['$status'];
$bedrooms = $_POST['bedrooms'];
$bathrooms = $_POST['bathrooms'];
$minPrice = $_POST['minPrice'];
$maxPrice = $_POST['maxPrice'];
$result=mysqli_query($con,"SELECT * FROM properties WHERE type = '$type' and status = '$status' and bedrooms='$bedrooms' and bathrooms = '$bathrooms' and price between $minPrice and $maxPrice");
如果你的$ result上方的代码没问题,它将会起作用
答案 3 :(得分:0)
试试这个:
<?php
$type = $_POST['type'];
$status = $_POST['status'];
$minPrice = $_POST['min_price'];
$maxPrice = $_POST['max_price'];
$bedrooms = $_POST['bedrooms'];
$baths = $_POST['baths'];
$con=mysqli_connect("localhost","root","","house");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM properties WHERE tipo='$type' and baths = '$baths' and bedrooms = '$bedrooms' and status = '$status' and price between '$minPrice' and '$maxPrice'");
while($row = mysqli_fetch_array($result)) {
echo $row['type'] . " " . $row['status'];
echo "<br>";
}
mysqli_close($con);
我在这里创建了一个表,它正在运行。 不要忘记更改HTML代码&#34;标记并添加属性&#34; name&#34;