简体   繁体   English

使用PHP从两个表中显示数据

[英]Displaying data from two tables using PHP

I am trying to display data from two tables from phpMyadmin database. 我试图显示来自phpMyadmin数据库的两个表中的数据。 I have a website with a form where you put what information are you looking for and then this information should be displayed from two tables in the database. 我有一个带有表单的网站,您可以在其中放置要查找的信息,然后应该从数据库的两个表中显示该信息。 I know I have to use a SQL JOIN, I have tried different ways and cannot figure this out. 我知道我必须使用SQL JOIN,我尝试了不同的方法,但无法弄清楚。 I will appreciate any help 我将不胜感激

Here is my php code: 这是我的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>";
?>

I got it to work. 我知道了。

I used 我用了

SELECT *
FROM INBOUND_FLIGHT, OUTBOUND_FLIGHT
...

and then changed the echo display 然后更改回显显示

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

Put a space after '$from' in your query section below: 在下面的查询部分中,在'$from'之后放置一个空格:

    AND INBOUND_FLIGHT.AIRCRAFT_FROM = '$from'AND

as

    AND INBOUND_FLIGHT.AIRCRAFT_FROM = '$from' AND

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM