如何在php中用多选或文本框搜索数据库中的数据

时间:2013-11-16 07:22:35

标签: php mysql

当用户按类别搜索时,如果用户想要通过移动搜索,则显示数据,则显示数据。

用户可以按类别或状态或移动设备搜索任何记录。

这是我试过的代码。

     <?php
     if(isset($_POST['submit']))
 {
   $status = $_POST['status'];
  $priority = $_POST['priority'];
   $ticket = $_POST['ticket'];
    $mobile = $_POST['mobile'];
      echo $mobile;
    $query = "SELECT * FROM ticketreg WHERE 1";
    if(isset($status) && $status != '') {
   $query .= " And status Like'%$status%'";
   }
    if(isset($priority) && $priority != '') {
   $query .= " OR priority Like'%$priority%'";
    }
   if(isset($ticket) && $ticket != '') {
   $query .= " OR ticket Like '%$ticket%'";
  }
   if(isset($mobile) && $mobile != '') {
  $query .= " OR EMAILID Like '%$mobile%'";
 }




$result = mysql_query($query); 


while($row = mysql_fetch_array($query))
 {



 ?>

请给我一些暗示。这里是html代码。

   <form name="frmSearch" method="post" action="searchresult.php">
      <table width="100%" cellspacing="0" cellpadding="0" border="0" >
  <tbody>
    <tr>
    <td class="singup-text">Date : </td><td width="5"></td>
    <td> <span style="font-size:12px; margin-right:3px;">From </span><input id="date"            name="date" class="postsellbox1" style="width:75px; " type="text">
  <span style="font-size:12px; margin-left:2px; margin-right:2px;">to </span> <input    id="date"  name="date" class="postsellbox1" style="width:75px; " type="text"> 

   </td>
     <td class="singup-text" >Status : </td><td width="5"></td>
     <td><select id="StatusDropDown" name="status" class="postsellbox"    style="width:130px;" >

           <option value="101">--Select--</option>
          <option value="search">pending</option>
          <option value="search">Assigned</option>
        <option value="search">Cancelled</option>
        <option value="104">Closed</option>

        </select></td>
        <td class="singup-text" >Priority : </td><td width="5"></td>
          <td><select id="PriorityDropDown" name="priority" class="postsellbox" style="width:130px;" >

           <option value="101">--Select--</option>
          <option value="101">High</option>
          <option value="102">Medium</option>
          <option value="103">Normal</option>


        </select></td></tr>
      <tr><td height="15"></td></tr>
      <tr><td class="singup-text" >Category : </td><td width="5"></td>
         <td><select id="CategoryDropDown" name="category" class="postsellbox" style="width:130px;" >

           <option value="100">--Select--</option>
        <option value="101">Electricity</option>
        <option value="102">Painting</option>
        <option value="103">Plumbing</option>
        <option value="104">Carpentry</option>

      </select></td>
      <td class="singup-text">Ticket : </td><td width="5"></td>
        <td><input id="ticket no."  name="ticket" class="postsellbox1" style="width:130px;" type="text" value=""></td>
          <td class="singup-text">Mobile : </td><td width="5"></td>
             <td><input id="Mobile"  name="mobile" class="postsellbox1" style="width:130px;" type="text"></td>
            </tr>
             <tr>

            <td><input class="submit" type="submit" value="Search" ></td>
           </tr>
                <tr>

         <td><input class="submit1" type="reset" value="Reset" ></td>
            </tr>
            </tbody></table>
             </form>

1 个答案:

答案 0 :(得分:0)

试试这个

 $query = "SELECT * FROM ticketreg WHERE 1";
    if(isset($status) && $status != '') {
   $query .= " And status Like'%$status%'";
   }
    if(isset($priority) && $priority != '') {
   $query .= " OR priority Like'%$priority%'";
    }
   if(isset($ticket) && $ticket != '') {
   $query .= " OR ticket Like '%$ticket%'";
  }
   if(isset($mobile) && $mobile != '') {
  $query .= " OR EMAILID Like '%$mobile%'";
 }

编辑:

 $query = "SELECT * FROM ticketreg WHERE ";
    if(isset($status) && $status != '') {
   $query .= "status Like'%$status%' OR";
   }
    if(isset($priority) && $priority != '') {
   $query .= " priority Like'%$priority%' OR ";
    }
   if(isset($ticket) && $ticket != '') {
   $query .= " ticket Like '%$ticket%' OR ";
  }
   if(isset($mobile) && $mobile != '') {
  $query .= " EMAILID Like '%$mobile%' OR ";
 }
 $query = substr($query,0,-3);