0

我正在尝试显示来自 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>";
?>
4

2 回答 2

1

我让它工作。

我用了

SELECT *
FROM INBOUND_FLIGHT, OUTBOUND_FLIGHT
...

然后改变了回声显示

echo "<td>" . $row['ARRIVAL_TIME'] . "</td>";
...
...
于 2012-11-16T19:24:23.517 回答
0

'$from'在下面的查询部分中放置一个空格:

    AND INBOUND_FLIGHT.AIRCRAFT_FROM = '$from'AND

作为

    AND INBOUND_FLIGHT.AIRCRAFT_FROM = '$from' AND
于 2012-11-16T17:25:40.053 回答