您好我正在尝试使用 OOP PHP 创建搜索功能,但是当我运行查询并输入错误数据时,我仍然得到结果。不在数据库中的结果。
我觉得我的代码中缺少一些东西,
也许我的查询是错误的我不确定,因为我是整个编程方面的新手。
欢迎任何帮助!
索引.php
<?php
include("classes/class.House.inc");
?>
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>UndergradPad</title>
<link rel="stylesheet" type="text/css" href="css/style.css" />
</head>
<body>
<div id="bodyWrapper">
<div id"header">
<img id="banner" alt="UnderGradPad Logo" src="images/banner.png"/>
</div> <!-- End header -->
<div id="search">
<h1>Find student accomodation</h1><br/>
<p> Location </p>
<form method="post" action="search.php" name="search" id="searchform">
<input type="text" name="term" id="searchinput"/>
<input type="submit" name="submit" id="searchsubmit" value=""/>
</form>
<div class="help">e.g. 'PO5' or 'Portsmouth'</div>
</div> <!--End search -->
</body>
</html>
类/class.House.inc
<?php
include("connect/class.Database.inc");
class House extends Database {
public function search (){
$query = "SELECT * FROM houses WHERE postcode like '%$term%'";
$result = $this->mysqli->query($query);
$num_result = $result->num_rows;
if($num_result > 0){
while($rows =$result->fetch_assoc()){
$this->data[]=$rows;
//print_r($rows);
}
return $this->data;
}
} else {
echo 'No Records Found';
}
} }
?>