ローカルサイトでMySQLのユーザごとにユニークな8桁のユーザ番号を使用する基本的な検索オプションがあります(例:ユーザ番号がユーザと一致する場合そのユーザー番号に関連付けられているすべてのレコードが表示されます)、検索が終了すると不要な結果が表示されます。たとえば、次のようなユーザーが検索された場合、12345678とユーザー出口が表示されます。結果を表示しますが、私は12345677の検索を行い、ユーザーは存在しないと言うことができます。User fountというメッセージが表示されますが、データは表示されません。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_ *ライブラリ」の使用を中止してください。 it.2の代わりに 'mysqli_ *'や 'PDO'を使います。そのデータが来るかどうかを調べるために 'mysql_ * 'の' num_rows() 'を使わなければなりません。 'SQL INJECTION'を防ぐために' prepared statements'を使用してください(新しいライブラリがあります) –
'Accounting' +' mysql_ * '+' SQL Injection' =問題 –