duangong937906 2013-11-25 08:31
浏览 51
已采纳

如何从数据库mysql中检索特定数据

I have a search.php in this I'm searching for three fields:

  • Form no.
  • Name
  • Telephone no.

But when I select a From no., from select option and enter in input a Telephone no. and press the search button it search for telephone no. instead of form no. I want a search like when I select From no. and enter a value of than it should search only for form no nothing else and like all below two option:

serach.php

<div class="tleft">
  <h2 class="tdilouge">Search Reports</h2><center>
  <table class="tbleft">
    <form action="searchbyform.php" method="get">
      <thead>
        <tr>
          <td>
             <select name="formnumber" id="formnumber" >
               <option>Form No.</option>                 
               <option>Name</option>             
               <option>Telephone No.</option>             

             </select>
          </td>
         <td><input type="text" name="formnumber" id="formnumber" placeholder="Enter" /></td>
          <td><input type="submit" value="Search" /></td>
        </tr>
      </thead>
    </form>
  </table></center>
</div>

And this is submit.php I have a proper connection of database and column is table name:

submit.php

<?php

   $formnumber = $_GET['formnumber'];
   $sql = "SELECT * FROM colomn WHERE FormNo = $formnumber OR ConsumerName = $formnumber OR TelephoneNo = $formnumber";
   $query = mysql_query( $sql );
          if(mysql_num_rows($query) == 0)
          {
          echo "no result found";
          }
          echo "<table>";

          echo "<thead></thead>";
                while( $row = mysql_fetch_array( $query ) )
                     {
                      echo "<tr></tr>";
                     }
          echo "</table>";
?>
  • 写回答

1条回答 默认 最新

  • douwu8524 2013-11-25 08:38
    关注

    You could just check which option the person has selected and depending on that selected option you could run the query that belong to that option. Give the options a value, like this:

    (You should change the select name because the textfield is already named formnumber)

              <select name="form" id="form" >
               <option value="form">Form No.</option>                 
               <option value="name">Name</option>             
               <option value="telephone">Telephone No.</option>             
    
             </select>
    

    So when you choose the option form no. , $_GET['form'] would be "form"

    So just use an IF to check the 3 options.

    EDIT: The query when the Form no. has been chosen, will look like this:

    "SELECT * FROM colomn WHERE FormNo = $formnumber"

    And for the name and telephone no. You should just change the column name.

    if($_get['form']="form"){
    $sql="SELECT * FROM colomn WHERE FormNo = $formnumber";
    }
    
    if($_get['form']="name"){
    $sql="SELECT * FROM colomn WHERE ConsumerName = $formnumber";
    }
    if($_get['form']="telephone"){
    $sql="SELECT * FROM colomn WHERE TelephoneNo = $formnumber";
    }
    

    Also dont use mysql. Use mysqli or PDO instead.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条