我在本地站點上有一個基本的搜索選項,它在MySQL中爲每個用戶使用唯一的8位數字用戶號碼,它起作用(例如:如果用戶號碼與用戶匹配將顯示與該用戶號碼相關的所有記錄),只是當搜索結束時我得到了不想要的結果,而不希望的結果是,例如,如果我搜索的用戶的搜索結果爲12345678,並且用戶退出,它將會顯示結果,但可以說我做了搜索12345677和用戶不存在我會得到消息用戶來源,但沒有數據顯示。PHP,MySQL - 簡單的搜索帶有不想要的結果
搜索表單代碼:
<form action="search.php" method="GET">
<input type="text" name="query" placeholder="Student Number" autofocus/>
<input type="submit" value="Search" />
</form>
的search.php
include_once("config.php");
$query = $_GET['query'];
$result = mysql_query("SELECT * FROM loan WHERE userno=$query");
if(empty($result)) {
// } else{
echo "<center><table border='0' id='3'>";
echo "<tr id='2'>";
echo "<td><center>System Message</center></td></tr>";
echo "<tr id='loan4'><br />";
echo "<td><center><h1>No Record Found</h1>";
echo "<br/><a href='index.php' class='button button1 link'>Go back</a>";
echo "<br/>OR</a>";
echo "<br/><a href='add_new_loan.php' class='button button1 link'>Add New Loan</a>";
echo "</center></td>";
echo "</tr>";
echo "</table></center>";}
else{
?>
<table width='95%' border='0' id='loan1'>
<tr id='loan2'>
<td width="120">User No.</td>
<td width="130">Full Name</td>
<td width="90">Amount</td>
<td width="100">aken</td>
<td width="100">Due</td>
<td width="248">Notes</td>
<td width="120" align="center">Options</td>
</tr>
<?php
while($res = mysql_fetch_array($result)) {
echo "<tr id='4'>";
echo "<td>".$res['userno']."</td>";
echo "<td><a href=\"receipt.php?id=$res[id]\" target=\"_blank\">".$res['name']." ".$res['surname']."</a></td>";
echo "<td>".$res['loana']."</td>";
echo "<td>".$res['datet']."</td>";
echo "<td>".$res['dated']."</td>";
echo "<td>".$res['notes']."</td>";
echo "</tr>";
}
echo "<center><table border='0' id='loan3'>";
echo "<tr id='loan2'>";
echo "<td><center>System Message</center></td></tr>";
echo "<tr id='4'><br />";
echo "<td><center><h1>Record Found</h1>";
echo "<br/><a href='index.php' class='button button1 link'>Go back</a>";
而且我已經試過!empty
但沒有喜悅,任何幫助是極大的讚賞。
1.停止使用'已棄用+已刪除的mysql_ *庫'。使用'mysqli_ *'或'PDO'代替它。你必須使用'mysql_ *'的'num_rows()'來檢查數據是否來臨。使用'prepared statements'來防止'SQL INJECTION'(新庫有這些) –
'Accounting' +'mysql_ *'+'SQL Injection' =有問題 –