duanchique1196 2017-10-04 11:08
浏览 18
已采纳

PHP,MySQL - 简单搜索不需要的结果[重复]

This question already has an answer here:

I have a basic search option on my local site which uses unique 8 digit user number for each user in the MySQL and it works (eg: if the user number is matched to user it will display all of the records associated with that user number) its just I get unwanted result when the search is concluded, and the unwanted result is that if for example I do a search for a user with the following no 12345678 and user exits it will show the result but lets say I do a search for 12345677 and user does not exist I will get the message User fount but no data displayed.

Search form code:

<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>";

Also I have tried !empty but no joy, Any help is greatly appreciated.

</div>
  • 写回答

3条回答 默认 最新

  • douyalin0847 2017-10-04 11:19
    关注

    Fix your current code by using mysql_num_rows() like below:-

    <?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>";
    }?>
    

    Note:-

    Stop using deprecated+removed mysql_* library. Use mysqli_* or PDO along with prepared statements to prevent from SQL INJECTION.

    REFREENCE:-

    mysqli_* with prepared statements

    PDO with prepared statements

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度