【发布时间】:2023-04-01 09:55:01
【问题描述】:
我有一个搜索表单,可以从数据库中搜索房产列表。在它工作正常之前,我能够显示搜索结果,然后突然之间它就没有显示出来。是不是我做错了什么。
这里是代码
<?php
require 'core/init.php';
////////////using mysqli to connect with database
$mysqli = new mysqli("localhost","root","", "test");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
///////////set search variables
$property = $_POST['property'];
$bedroom = $_POST['BedroomNumber'];
$bathroom = $_POST['BathroomNumber'];
$priceMin = $_POST['PriceMin'];
$priceMax = $_POST['PriceMax'];
$termlease = $_POST['TermLease'];
//////////search
if(isset($_POST['utilities']) && is_array($_POST['utilities'])) {
foreach($_POST['utilities'] as $check) {
//echoes the value set in the HTML form for each checked checkbox.
//so, if I were to check 1, 3, and 5 it would echo value 1, value 3, value 5.
//in your case, it would echo whatever $row['Report ID'] is equivalent to.
}
}
$sql = $mysqli->query("select * from propertyinfo where Property like '%$property%' and NumBed like '%$bedroom%' and NumBath like '%$bathroom%' and Price >= '$priceMin' and Price <= '$priceMax' and utilities like '%$check%' and TermLease like '%$termlease%'");
if($sql === FALSE) {
die(mysql_error()); // TODO: better error handling
}
?>
显示结果的部分
<?php
if($sql->num_rows){
while ($row = $sql->fetch_array(MYSQLI_ASSOC)){
echo '<div id="listing">
<div id="propertyImage">
<img src="uploadimages/'.$row['imageName1'].'" width="200" height="150" alt=""/>
</div>
<div id="basicInfo">
<h2>$'.$row['Price'].'</h2>
<p style="font-size: 18px;"># '.$row['StreetAddress'].', '.$row['City'].', BC</p>
<p>'.$row['NumBed'].' Bedrooms | '.$row['NumBath'].' Bathrooms | '.$row['Property'].'</p>
<br>
<p><a href="outputtest2.php?record_id='.$row['ID'].'" class="link2" target="_blank">View Full Details</a> | <a href="" class="link2">Get Directions</a>
</div>
</div>';
}
}
else
{
echo '<h2>0 Search Results</h2>';
}
?>
谢谢
【问题讨论】:
-
缩进、格式化的代码会更容易阅读。 :)
-
“突然之间”是什么意思?你最后改变了什么?
-
我注意到你所有的
$_POSTs 都以大写字母开头,除了property。可能不是问题,但永远不知道。也可以保持一致。 -
如果你改用
$sql = mysqli_query("select * from propertyinfo...会怎样? -
请不要滥用 mysqli 我的意思是使用 mysqli 并不意味着您的代码可以安全地免受 sql 注入您仍然需要转义所有请求...在我的建议中使用准备好的查询和 pdo stackoverflow.com/q/12859942/1723893