试图让PHP表单查询SQL数据库使用多个if语句


Trying to get PHP form to query SQL database using multiple if statements

第一次来。我试图使用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>

你的php开始和结束标签缺失-

function html_form()
{

Do it——

<?php
function html_form()
{

HTML不识别这些php方法,请按照前面代码中所做的方式进行操作。

你需要将逻辑部分与视图分开,按照下面提到的方式来做(还有很多其他的方法)

<?php
//PHP code chunks
?>
HTML Part
<?php
//Another PHP code chunks
?>
Again your HTML part

您需要确保所有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的打开/关闭标记。

我同意其他的答案。你丢失了PHP的开始和结束标签。

<?php ?>

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