我正在尝试从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>";
?>
答案 0 :(得分:1)
我得到了它的工作。
我用过
SELECT *
FROM INBOUND_FLIGHT, OUTBOUND_FLIGHT
...
然后更改了回显显示
echo "<td>" . $row['ARRIVAL_TIME'] . "</td>";
...
...
答案 1 :(得分:0)
在'$from'
后面的查询部分中添加空格:
AND INBOUND_FLIGHT.AIRCRAFT_FROM = '$from'AND
作为
AND INBOUND_FLIGHT.AIRCRAFT_FROM = '$from' AND