簡體   English   中英

使用PHP從兩個表中顯示數據

[英]Displaying data from two tables using PHP

我試圖顯示來自phpMyadmin數據庫的兩個表中的數據。 我有一個帶有表單的網站,您可以在其中放置要查找的信息,然后應該從數據庫的兩個表中顯示該信息。 我知道我必須使用SQL JOIN,我嘗試了不同的方法,但無法弄清楚。 我將不勝感激

這是我的PHP代碼:

    <?php
//connect to database:
$con = mysql_connect("SERVER", "LOGIN", "PASS") or die("Connection Failed");
mysql_select_db("TABLE", $con) or die ("Connection Failed");

//create a PHP variable to hold the value from HTML form
$from = $_POST['arrival'];
$to = $_POST['destination'];
$time = $_POST['time'];
$tailnumber = $_POST['tailnumber'];

$error_status = false;

// validating input
if (empty($_POST['arrival'])){
echo "Please enter the origin.";
$error_status = true;
} 
if (empty($_POST['destination'])){
echo "</br></br> Please enter the destination.";
$error_status = true;
} 
if (empty($_POST['time'])){
echo "</br></br> Please enter the date.";
$error_status = true;
} 
if (empty($_POST['tailnumber'])){
echo "</br></br> Please enter the tail number.";
$error_status = true;
} 

//create the query for selecting all the records which equal to the value from the form
if(!$error_status) {
$query = "SELECT *
FROM INBOUND_FLIGHT
INNER JOIN OUTBOUND_FLIGHT 
ON INBOUND_FLIGHT.TAIL_NUMBER = OUTBOUND_FLIGHT.TAIL_NUMBER
WHERE INBOUND_FLIGHT.ARRIVAL_TIME = '$time' AND INBOUND_FLIGHT.TAIL_NUMBER = '$tailnumber' AND INBOUND_FLIGHT.AIRCRAFT_FROM = '$from'AND OUTBOUND_FLIGHT.AIRCRAFT_TO = '$to'";

//run query
$result = mysql_query($query);
// creating a table
echo "<table border='1'>
<tr>
<th>ARRIVAL TIME</th>
<th>AIRCRAFT TYPE</th>
<th>TAIL NUMBER</th>
<th>CREW NAME</th>
<th>ORIGIN</th>
<th>DESTINATION</th>
</tr>";

//Print the record that matches the criteria of the query
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "<tr>";
echo "<td>" . $row['INBOUND_FLIGHT.ARRIVAL_TIME'] . "</td>";
echo "<td>" . $row['INBOUND_FLIGHT.AIRCRAFT_TYPE' ] . "</td>";
echo "<td>" . $row['INBOUND_FLIGHT.TAIL_NUMBER'] . "</td>";
echo "<td>" . $row['INBOUND_FLIGHT.CREW_NAME'] . "</td>";
echo "<td>" . $row['INBOUND_FLIGHT.AIRCRAFT_FROM'] . "</td>";
echo "<td>" . $row['OUTBOUND_FLIGHT.AIRCRAFT_TO'] . "</td>";
}}
//close the connection
mysql_close($con);
echo "</table>";
?>

我知道了。

我用了

SELECT *
FROM INBOUND_FLIGHT, OUTBOUND_FLIGHT
...

然后更改回顯顯示

echo "<td>" . $row['ARRIVAL_TIME'] . "</td>";
...
...

在下面的查詢部分中,在'$from'之后放置一個空格:

    AND INBOUND_FLIGHT.AIRCRAFT_FROM = '$from'AND

    AND INBOUND_FLIGHT.AIRCRAFT_FROM = '$from' AND

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM