我無法讓我的搜索功能正常工作。我沒有看到任何PHP錯誤,但頁面刷新並重新顯示數據庫列。我希望它只顯示搜索查詢的結果。
例如,如果我搜索「邁克」我想在「Tech_Num」顯示所有米凱什,「Tech_F_Name」,「Tech_L_Name」和「Mobile_Num」。
代碼:
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
//Include the connection
include "connect.php";
$sql = "SELECT * FROM tech_info ";
if (isset($_POST['searchquery'])) {
$result = mysqli_query($con, "SELECT * FROM tech_info");
$search_term = $_POST['searchquery'];
$sql .= "WHERE Tech_F_Name = '{$search_term}'";
$sql .= " OR Tech_L_Name = '{$search_term}'";
}
?>
<form action="test2.php" method="POST">
Search: <input type="text" name="searchquery" />
<input type="submit" name="searchname" value="Search Me">
</form>
<table width="70%" cellpadding="5" cellspace="5">
<tr>
<td><strong>Tech_Num</strong></td>
<td><strong>First Name</strong></td>
<td><strong>Last Name</strong></td>
<td><strong>Mobile Number</strong></td>
</tr>
<?php
while ($row = mysqli_fetch_array($result)) {
?>
<tr>
<td><?php echo $row['Tech_Num']; ?>
<td><?php echo $row['Tech_F_Name']; ?>
<td><?php echo $row['Tech_L_Name']; ?>
<td><?php echo $row['Mobile_Num']; ?>
<?php } ?>
</table>
僅供參考,您可以[SQL注入](http://stackoverflow.com/q/60174) –
您實際上並未運行整個查詢。 *在運行查詢之後,將where子句附加到查詢的部分會發生*。實際上,這三條線根本就什麼都不做。 –