我一直在尝试使用 PHP 连接到 MySQL 并显示一些数据。我对此完全陌生。我主要从 w3schools 和其他一些在 Google 上找到的网站阅读了这个主题。但是我有 2 个问题 1st,PHP 代码之后开始
$result = mysql_query("SELECT * FROM Passengers_On_Flight");
正在显示为纯文本。
另一个问题是,即使没有提交表单,它也总是会发生,这让我相信表单由于某种原因if(isset($_POST['submit']))
没有被正确评估。我发现了其他问题,人们遇到的问题是它从不评估真实,但并不总是评估真实。
我在我的服务器上做了一个 PHP -v 以确保安装了 php5,我也这样做了: http: //www.gilesorr.com/papers/PHP/x37.html
两者都表明 PHP 实际上已安装并正在运行。我已经搜索过,但似乎无法找出问题所在。
这是我使用 PHP 编写的 HTML:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<HTML>
<link rel="stylesheet" type="text/css" href="cobraStyle.css">
<HEAD>
<TITLE>Cobra</TITLE>
</HEAD>
<BODY>
<div id="container">
<div id="header">
<h1>
Cobra Airlines
</h1>
</div>
<div id="navigation">
<ul>
<li><a href="#">Home</a></li>
<li><a href="viewFlights.html">Check Flights</a></li>
<li><a href="#">Administration</a></li>
<li><a href="#">Contact us</a></li>
</ul>
</div>
<div id="content">
<h2>
Flights heading
</h2>
<?php
if($_POST['submit'])
{
$con = mysql_connect("localhost","root","qwerty");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("Cobra_Airlines", $con);
$result = mysql_query("SELECT * FROM Passengers_On_Flight");
echo "<table border='1'>
<tr>
<th>Fligh Number</th>
<th>Departure Date</th>
<th>First Name</th>
<th>Last Name</th>
<th>Passenger Number</th>
</tr>";
while($row = mysql_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['FlightNum'] . "</td>";
echo "<td>" . $row['Departure_Date'] . "</td>";
echo "<td>" . $row['First_Name'] . "</td>";
echo "<td>" . $row['Last_Name'] . "</td>";
echo "<td>" . $row['PassengerNum'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
}
?>
<p>
<form name="input" action="viewFlights.html" method="post">
Flight Number: <input type="text" name="flight">
<input type="submit" value="submit" name="submit">
</form>
</p>
</div>
<div id="footer">
Copyright © Cobra Air, 2012
</div>
</div>
</BODY>
</HTML>
这是更新的PHP:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<HTML>
<link rel="stylesheet" type="text/css" href="cobraStyle.css">
<HEAD>
<TITLE>Cobra</TITLE>
</HEAD>
<BODY>
<div id="container">
<div id="header">
<h1>
Cobra Airlines
</h1>
</div>
<div id="navigation">
<ul>
<li><a href="#">Home</a></li>
<li><a href="viewFlights.html">Check Flights</a></li>
<li><a href="#">Administration</a></li>
<li><a href="#">Contact us</a></li>
</ul>
</div>
<div id="content">
<h2>
Flights heading
</h2>
<?php
$con = mysql_connect("localhost","root","qwerty");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("Cobra_Airlines", $con);
$result = mysql_query("SELECT * FROM Passengers_On_Flight");
echo
"<table border='1'>
<tr>
<th>Fligh Number</th>
<th>Departure Date</th>
<th>First Name</th>
<th>Last Name</th>
<th>Passenger Number</th>
</tr>";
while($row = mysql_fetch_array($result))
echo "<tr>";
echo "<td>" . $row['FlightNum'] . "</td>";
echo "<td>" . $row['Departure_Date'] . "</td>";
echo "<td>" . $row['First_Name'] . "</td>";
echo "<td>" . $row['Last_Name'] . "</td>";
echo "<td>" . $row['PassengerNum'] . "</td>";
echo "</tr>";
endwhile;
echo "</table>";
mysql_close($con);
?>
<p>
<form name="input" action="viewFlights.php" method="post">
Flight Number: <input type="text" name="flight">
<input type="submit" value="submit" name="submit">
</form>
</p>
</div>
<div id="footer">
Copyright © Cobra Air, 2012
</div>
</div>
</BODY>
</HTML>