0

第一次来这里。我正在尝试使用 PHP/HTML 表单通过数据库进行查询,然后打印找到的结果的输出。问题是,我在最后一行代码(ish)上不断收到语法错误,它从后面的行</html>变为</body>标签。我知道它必须是一些我想念的简单的东西,但我已经认真地看了 4 个小时并且无法弄清楚。任何帮助将不胜感激。谢谢!

这是代码:

<html>
<head>
<title>Audit Activity Report</title>
</head>
<body>
<h4>Audit Activity Report</h4>

<?php
 if (!$_REQUEST['Submit']) { 
   html_form(); 
} else
{ 
 select_cd();  
 }
?>
function html_form()
{

<p>Please enter the fields you would like to run a report on:</p>

<form name="Audit Activity Report" method="post" action="<? echo $_SERVER['PHP_SELF']; ?>">
<p>
Employee BEMS: <input name="EmployeeBEMS" type="text" size="10" maxlength="10"> </p>
<p>Escort BEMS:
<input name="EscortBEMS" type="text" size="10" maxlength="10">
</p>
<p>Current Activity ID:
<input name="CurrentActivityID" type="text" size="20" maxlength="20">
</p>
<p>
<label for="LaborTraining">LaborTraining:</label>
<select name="LaborTraining2" id="LaborTraining">
<option>Yes</option>
<option>No</option>
</select>
</p>
<p>
<label for="EthicsTraining">Ethics Training:</label>
<select name="EthicsTraining" id="EthicsTraining">
<option>Yes</option>
<option>No</option>
</select>
</p>
<p>
<label for="WorkAuthorization">Work Authorization:</label>
<select name="WorkAuthorization" id="WorkAuthorization">
<option>Yes</option>
<option>No</option>
</select>
</p>
<p>
<label for="Predicted Outcome">Predicted Outcome:</label>
<select name="Predicted Outcome" id="PredictedOutcome">
<option>Yes</option>
<option>No</option>
</select>
</p>
<p>
<input type="submit" name="Submit" value="View Report" /> </p>
<p>
<input type="reset" name="Clear" id="Clear" value="Clear" /> </p>
</form>

}
function select_cd()
{

<h4>Report</h4>


/* set's the variables for MySQL connection */

$server = "******"; // this is the server address and port
$username = "*******"; // change this to your username
$password = "*****"; // change this to your password

/* Connects to the MySQL server */

$link = mysql_connect ($server, $username, $password)
or die (mysql_error());

/* Defines the Active Database for the Connection */

if (!mysql_select_db("a32****_Audit", $link))
{
echo "<p>There has been an error. This is the error message:</p>";
echo "<p><strong>" . mysql_error() . "</strong></p>";
echo "Please Contact Your Systems Administrator with details";
}

/* Sets the SQL Query */

$sql = "SELECT * FROM Audit_Activity_Log";
$sql .= " WHERE ( Audit_Activity_Log.Employee_BEMS_ID =
'{$_POST['EmployeeBEMS']}')";

$sql2 = "SELECT * FROM Audit_Activity_Log";
$sql2 .= " WHERE ( Audit_Activity_Log.Escort_BEMS_ID =
'{$_POST['EscortBEMS']}')";

$sql3 = "SELECT * FROM Audit_Activity_Log";
$sql3 .= " WHERE ( Audit_Activity_Log.Current_Activity_ID = '{$_POST['CurrentActivityID']}')";

$sql4 = "SELECT * FROM Audit_Activity_Log";
$sql4 .= " WHERE ( Audit_Activity_Log.Labor_Training= '{$_POST['LaborTraining']}')";

$sql4 = "SELECT * FROM Audit_Activity_Log";
$sql4 .= " WHERE ( Audit_Activity_Log.Ethics_Training= '{$_POST['EthicsTraining']}')";

$sql5 = "SELECT * FROM Audit_Activity_Log";
$sql5 .= " WHERE ( Audit_Activity_Log.Work_Authorization= '{$_POST['WorkAuthorization']}')";

$sql5 = "SELECT * FROM Audit_Activity_Log";
$sql5 .= " WHERE ( Audit_Activity_Log.Predicted_Outcome= '{$_POST['PredictedOutcome']}')";

/* Passes a Query to the Active Database */

$result = mysql_query($sql, $link);

if (!$result)
{
echo("<p>Error performing query: " . mysql_error() . "</p>");
exit();
}
/* Passes a Query to the Active Database */
$result2 = mysql_query($sql2, $link);

if (!$result2)
{
echo("<p>Error performing query: " . mysql_error() . "</p>");
exit();
}
/* Passes a Query to the Active Database */
$result3 = mysql_query($sql3, $link);

if (!$result3)
{
echo("<p>Error performing query: " . mysql_error() . "</p>");
exit();
}
/* Passes a Query to the Active Database */

$result4 = mysql_query($sql4, $link);

if (!$result4)
{
echo("<p>Error performing query: " . mysql_error() . "</p>");
exit();
}
/* Passes a Query to the Active Database */
$result5 = mysql_query($sql5, $link);

if (!$result5)
{
echo("<p>Error performing query: " . mysql_error() . "</p>");
exit();
}
/* Starts the table and creates headings */
echo "<table border='1'>
<tr>
<th>Employee BEMS</th>
<th> Escort BEMS</th>
<th> Current Activity ID</th>
<th> Labor Training</th>
<th>Ethics Training</th>
<th>Work Authorization</th>
<th>Predicted Outcome</th>
</tr>";

/* Retrieves the rows from the query result set
and puts them into a HTML table row */

if ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo("<tr>\n<td>" . $row["Employee_BEMS_ID"] . "</td>");
echo("<td>" . $row["Escort_BEMS_ID"] . "</td>");
echo("<td>" . $row["Current_Activity_ID"] . "</td>");
echo("<td>" . $row["Labor_Training"] . "</td>");
echo("<td>" . $row["Ethics_Training"] . "</td>");
echo("<td>" . $row["Work_Authorization"] . "</td>");
echo("<td>" . $row["Predicted_Outcome"] . "</td>\n</tr>\n\n");
}
elseif ($row = mysql_fetch_array($result2, MYSQL_ASSOC))
{
echo("<tr>\n<td>" . $row["Employee_BEMS_ID"] . "</td>");
echo("<td>" . $row["Escort_BEMS_ID"] . "</td>");
echo("<td>" . $row["Current_Activity_ID"] . "</td>");
echo("<td>" . $row["Labor_Training"] . "</td>");
echo("<td>" . $row["Ethics_Training"] . "</td>");
echo("<td>" . $row["Work_Authorization"] . "</td>");
echo("<td>" . $row["Predicted_Outcome"] . "</td>\n</tr>\n\n");
}
elseif ($row = mysql_fetch_array($result3, MYSQL_ASSOC))
{
echo("<tr>\n<td>" . $row["Employee_BEMS_ID"] . "</td>");
echo("<td>" . $row["Escort_BEMS_ID"] . "</td>");
echo("<td>" . $row["Current_Activity_ID"] . "</td>");
echo("<td>" . $row["Labor_Training"] . "</td>");
echo("<td>" . $row["Ethics_Training"] . "</td>");
echo("<td>" . $row["Work_Authorization"] . "</td>");
echo("<td>" . $row["Predicted_Outcome"] . "</td>\n</tr>\n\n");
}
elseif ($row = mysql_fetch_array($result4, MYSQL_ASSOC))
{
echo("<tr>\n<td>" . $row["Employee_BEMS_ID"] . "</td>");
echo("<td>" . $row["Escort_BEMS_ID"] . "</td>");
echo("<td>" . $row["Current_Activity_ID"] . "</td>");
echo("<td>" . $row["Labor_Training"] . "</td>");
echo("<td>" . $row["Ethics_Training"] . "</td>");
echo("<td>" . $row["Work_Authorization"] . "</td>");
echo("<td>" . $row["Predicted_Outcome"] . "</td>\n</tr>\n\n");
}
elseif ($row = mysql_fetch_array($result5, MYSQL_ASSOC))
{
echo("<tr>\n<td>" . $row["Employee_BEMS_ID"] . "</td>");
echo("<td>" . $row["Escort_BEMS_ID"] . "</td>");
echo("<td>" . $row["Current_Activity_ID"] . "</td>");
echo("<td>" . $row["Labor_Training"] . "</td>");
echo("<td>" . $row["Ethics_Training"] . "</td>");
echo("<td>" . $row["Work_Authorization"] . "</td>");
echo("<td>" . $row["Predicted_Outcome"] . "</td>\n</tr>\n\n");
}

</body>
</html>
4

3 回答 3

0

您的 php 开始和结束标记丢失 -

function html_form()
{

这样做——

<?php

function html_form()
{

HTML无法识别这些php方法,就像您之前在代码中所做的那样。

您需要将逻辑部分与视图分开,按照下面提到的方式进行操作(还有许多其他方法)

<?php

//PHP code chunks

?>
HTML Part
<?php

//Another PHP code chunks

?>
Again your HTML part
于 2013-05-01T04:08:25.553 回答
0

您需要确保所有 PHP 代码都在 PHP 标记内。您可以关闭 PHP 标记以编写原始 HTML。这是一个示例,来自您的代码:

<?php
 if (!$_REQUEST['Submit']) { 
   html_form(); 
} else
{ 
 select_cd();  
 }
?>
function html_form()
{

您需要更改它,因为它function html_form() {是 PHP,但它在 php 标签之外。试试这样:

<?php
 if (!$_REQUEST['Submit']) { 
   html_form(); 
} else
{ 
 select_cd();  
 }

function html_form()
{
?>

检查所有代码,确保所有 PHP 代码都在<?php ?>标签内。

为了将来参考,当您遇到 PHP 错误时,请务必告诉我们错误消息的内容,并准确找出错误发生的位置。这一点很重要。

您在文件末尾描述的错误是因为您最后一组 PHP 标记未关闭。按照我的解释检查您的代码,您将修复错误。修复错误消息后,还要查看您的输出页面。如果您在浏览器中看到任何看起来像 PHP 的东西,那么您错过了一些 PHP 打开/关闭标签。

于 2013-05-01T04:15:49.770 回答
0

我同意其他答案。您缺少打开和关闭 PHP 标记。

<?php ?>

如果您使用 Apache,另一个调试的好方法是您的 Apache error_log。帮了我几次。

于 2013-05-01T04:22:39.353 回答