如何在 mysql 中的两个表中在 php 中创建搜索功能


How to create a search functionality in php from two tables in mysql

我目前在某个主题的项目上遇到了这个问题。我想知道是否可以从 mysql 中的两个表创建一个搜索函数。我真的无法用语言解释它,所以我将为此提供代码。

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

这是两个表的代码和图像。

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