使用PHP本机按学生实体ID搜索

时间:2014-01-28 16:32:21

标签: php mysql search

我对搜索框有疑问:我有一个搜索txtbox,会搜索课程schedule

我正在做的是搜索student id,它会像搜索ID号2004-020392003-2938一样成功运行。

当我搜索44之类的号码时,它成功地说“找不到结果”。这意味着它是正确的。但我的问题是,当我搜索1时,它会显示整体,当我搜索2时,它会再次显示整个数据。我希望当我搜索1时,它会转到“找不到结果”。

有人能帮助我吗?我的student_id类型为varchar,我使用脚本LIKE搜索数据库中的相同名称。 (对不起,我正在使用旧的mysql_query)。

这是我的代码:

<div class="news_box">
      <center><h2>My Class Schedule </h2>   </center>   
      <div id="tfheader">
        <form id="tfnewsearch" action="schedule.php?" method="get">
          <input type="text"  name="search" placeholder="ID Number" size="21" maxlength="120" />
          <input type="submit" value="search" class="tfbutton" />
        </form>

        <div class="tfclear"></div>
      </div>

        <a href="#"></a>

        <p><center>
        <?php require("scripts/DataConnect.php"); ?>
        <?php include("search.php");?>

        <table width="728" height="121" border="1" cellspacing="0" bordercolor="#888" style="border-collapse: collapse; border-top-color: rgb(255, 255, 255); border-right-color: rgb(255, 255, 255); border-bottom-color: rgb(255, 255, 255); border-left-color: rgb(255, 255, 255); border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; ">
        <tr>
          <th>Offer Code </th>
          <th>Subject</th>
          <th>Day</th>
          <th>Time</th>
          <th>Units</th>
          <th>Room</th>
        </tr>
        <?php
          if($_GET['search'] != ''){
            if($row_count != 0){
                while($row = mysql_fetch_array($res)){ 
                 echo  "<tr>
                        <td>".$row['offer_code']."</td>
                        <td>".$row['subject']."</td>
                        <td>".$row['time']."</td>
                        <td>".$row['day']."</td>
                        <td>".$row['units']."</td>
                        <td>".$row['room_number']."</td>
                       </tr>";
                }
              }else{
                echo "<tr>
                        <td colspan='6' style='text-align:center;'>No RESULTS FOUND.</td>
                      </tr>";
              }
          }else{
            echo "<tr>
                    <td colspan='6' style='text-align:center;'>PLEASE SEARCH CLASS SCHEDULE.</td>
                  </tr>";
          }
        ?>
        </table>
        </center></p>
        <div class="cleaner"></div>
        </div>

我的search.php文件:

<?php
  error_reporting(E_ALL ^ E_NOTICE);

  $search = mysql_real_escape_string($_GET['search']);
  $query = "SELECT 
              student_record.student_id,
              class_schedule.student_id,
              class_schedule.offer_code,
              class_schedule.subject,
              class_schedule.time,
              class_schedule.day,
              class_schedule.units,
              class_schedule.room_number
              FROM
              student_record
              INNER JOIN class_schedule ON class_schedule.student_id = student_record.student_id
              WHERE 
              student_record.student_id LIKE '%".$search."%'
                        ";
  $res = mysql_query($query);
  $row_count = mysql_num_rows($res);
?>

2 个答案:

答案 0 :(得分:1)

这是因为你正在使用LIKE,试试这个:

<?php
  error_reporting(E_ALL ^ E_NOTICE);

  $search = mysql_real_escape_string($_GET['search']);
  $query = "SELECT 
              student_record.student_id,
              class_schedule.student_id,
              class_schedule.offer_code,
              class_schedule.subject,
              class_schedule.time,
              class_schedule.day,
              class_schedule.units,
              class_schedule.room_number
              FROM
              student_record
              INNER JOIN class_schedule ON class_schedule.student_id = student_record.student_id
              WHERE 
              student_record.student_id ='".$search."'
                        ";
  $res = mysql_query($query);
  $row_count = mysql_num_rows($res);
?>

答案 1 :(得分:-1)

你应该尝试 mysqli 模块(PHP Native),这太简单了,我的博客上是example

我希望这有帮助!