2017-10-04 21 views
0

ローカルサイトで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']."&nbsp;".$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試してみましたが、何の喜びは、すべてのヘルプは大歓迎されません。

+3

1.「廃止+削除されたmysql_ *ライブラリ」の使用を中止してください。 it.2の代わりに 'mysqli_ *'や 'PDO'を使います。そのデータが来るかどうかを調べるために 'mysql_ * 'の' num_rows() 'を使わなければなりません。 'SQL INJECTION'を防ぐために' prepared statements'を使用してください(新しいライブラリがあります) –

+0

'Accounting' +' mysql_ * '+' SQL Injection' =問題 –

答えて

1

は、以下のようなmysql_num_rows()を使用して、現在のコードを修正: -

<?php 
include_once("config.php"); 
$query = $_GET['query']; 
$result = mysql_query("SELECT * FROM loan WHERE userno=$query"); 
if(mysql_num_rows($result) >0) { 
?> 
    <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']."&nbsp;".$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>"; 
    } 
}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>"; 
}?> 

注: -

停止をdeprecated+removed mysql_* libraryを使用します。 mysqli_*またはPDOprepared statementsとともに使用して、SQL INJECTIONを防止してください。

REFREENCE: -

mysqli_* with prepared statements

PDO with prepared statements

1

$結果が完全に空ではない可能性があります。

mysql_num_rows()を使用してください。

例:

if (mysql_num_rows($result)==0) { 
     //PERFORM ACTION 
    } 
1

あなたがmysql_*機能の使用を停止する必要がある最初の事、彼らは減価償却と完全にPHP 7プリペアドステートメントを持つユーザーmysqli_*やPDOのように除去されています。 SQLインジェクションを防ぐために準備された文を利用して次に

Why shouldn't I use mysql_* functions in PHP?

は、以下のリンクを参照してください。

How can I prevent SQL injection in PHP?

プリペアドステートメントを使用するには、上記のリンクに従ってください。

config.phpの

<?php 
$servername = "localhost"; 
$username = "username"; 
$password = "password"; 
$dbname = "myDB"; 

// Create connection 
$conn = new mysqli($servername, $username, $password, $dbname); 
// Check connection 
if ($conn->connect_error) { 
    die("Connection failed: " . $conn->connect_error); 
} 
?> 

検索:

これはあなたのコードがどのように見えるべきかです。php

<?php 


include_once("config.php"); 
$query = $_GET['query']; 

$sql = "SELECT * FROM loan WHERE userno= $query "; // use prepared statements from the link I provided above 

$result = $conn->query($sql); 

if ($result->num_rows > 0) { 
    //Records found print the table 
    //OUTPUT THE TABLE AND HEADINGS HERE BEFORE THE LOOP 
    while($row = $result->fetch_assoc()) { 
     //fetch and output each data of row to your table td's 

    } 


}else{ 

//user data not found print message 

} 
関連する問題