2016-02-29 29 views
-1

我目前有這個問題在我的項目在某個主題。我想知道是否可以從mysql中的兩個表創建一個搜索功能。 我不能用文字來解釋它,所以我會爲此提供代碼。如何在php中的兩個表中創建一個搜索功能

<table class="table table-striped"> 
       <th>Date of Request</th> 
       <th>Brand</th> 
       <th>Model</th> 
       <th>Plate Number</th> 
       <th>Problem Description</th> 
       <th>Parts Replaced</th> 
       <th>Date Repair Completed</th> 


      <th></th> 
      <?php 
       require_once("db_open.php"); 
       $sql = "SELECT v.*, rt.* FROM vehicle v, repair_transaction rt WHERE v.Vehicle_Id = rt.Vehicle_Id 
       ORDER BY Date_of_repair_request ASC"; 
       $result = $conn->query($sql) or die($conn->error); 
       if ($result->num_rows > 0) { 
        while($row = $result->fetch_assoc()) { 

         echo "<tr>"; 
         echo "<td>".$row["Date_of_repair_request"]."</td>"; 
         echo "<td>".$row["Brand"]."</td>"; 
         echo "<td>".$row["Model"]."</td>"; 
         echo "<td>".$row["Plate_No"]."</td>"; 
         echo "<td>".$row["Problem_Description"]."</td>"; 
         echo "<td>".$row["Parts_replaced"]."</td>"; 
         echo "<td>".$row["Date_Repair_Completed_by_Maintenance"]."</td>"; 

         echo "</tr>"; 
        } 
       } else { 
        echo "<p>No transaction to show...</p>"; 
       } 

這是兩張表的代碼和圖像。

+0

display image ... –

+0

您期待什麼搜索功能?你想用值來搜索mysql表嗎? – rahul

+0

對不起,我只是新..我不知道如何顯示圖像呢。 – XyL3mZ

回答

0
 <table class="table table-striped"> 
      <thead> 
      <tr>  
       <th>Date of Request</th> 
       <th>Brand</th> 
       <th>Model</th> 
       <th>Plate Number</th> 
       <th>Problem Description</th> 
       <th>Parts Replaced</th> 
       <th>Date Repair Completed</th> 
       <th></th> 
      </tr> 
      <tr> 
      <form action="" method="post">  
       <th></th> 
       <th><input type="text" name="search_brand"></th> 
       <th>Input for modal</th> 
       <th>Input for Plate Number</th> 
       <th>Input for Problem Description</th> 
       <th>Input for Parts Replaced</th> 
       <th>Input for Date Repair Completed</th> 
       <th><input type="submit" name="search" value="search"></th> 
      </form> 
      </tr> 
      </thead> 
     <?php 
      require_once("db_open.php"); 
      if(isset($_POST['search_brand'])) 
      { 
       $search_brand = $_POST['search_brand']; 
       $search_brand_cond = ($search_brand!="")?" v.brand='$search_brand'":"1=1"; 
      } 
      else 
      { 
       $search_brand_cond = "1=1"; 
      } 

      // You can use same code for all input 

      $sql = "SELECT v.*, rt.* FROM vehicle v, repair_transaction rt WHERE v.Vehicle_Id = rt.Vehicle_Id AND $search_brand_cond 
      ORDER BY Date_of_repair_request ASC"; 
      $result = $conn->query($sql) or die($conn->error); 
      if ($result->num_rows > 0) { 
       while($row = $result->fetch_assoc()) { 

        echo "<tr>"; 
        echo "<td>".$row["Date_of_repair_request"]."</td>"; 
        echo "<td>".$row["Brand"]."</td>"; 
        echo "<td>".$row["Model"]."</td>"; 
        echo "<td>".$row["Plate_No"]."</td>"; 
        echo "<td>".$row["Problem_Description"]."</td>"; 
        echo "<td>".$row["Parts_replaced"]."</td>"; 
        echo "<td>".$row["Date_Repair_Completed_by_Maintenance"]."</td>"; 

        echo "</tr>"; 
       } 
      } else { 
       echo "<p>No transaction to show...</p>"; 
      } 
     ?> 
相關問題