我目前在某个主题的项目中遇到此问题。我想知道是否可以从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>";
}
这是两个表的代码和图像。
答案 0 :(得分: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>";
}
?>