douxun4860 2013-05-01 04:04
浏览 49

尝试使用多个if语句来获取PHP表单来查询SQL数据库

First timer here. I am trying to use a PHP/HTML form to query by database and then print an output of the results that were found. Problem is, I keep getting a syntax error on the last line of code(ish) it changes from being the line after </html> to the </body> tags. I know that it has to be something simple that I am missing but I have seriously looked at this for 4 hours and cannot figure it out. Any help would be kindly appreciated. Thanks!

Here is the code:

<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>
<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>
</tr>

");
}
elseif ($row = mysql_fetch_array($result2, MYSQL_ASSOC))
{
echo("<tr>
<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>
</tr>

");
}
elseif ($row = mysql_fetch_array($result3, MYSQL_ASSOC))
{
echo("<tr>
<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>
</tr>

");
}
elseif ($row = mysql_fetch_array($result4, MYSQL_ASSOC))
{
echo("<tr>
<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>
</tr>

");
}
elseif ($row = mysql_fetch_array($result5, MYSQL_ASSOC))
{
echo("<tr>
<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>
</tr>

");
}

</body>
</html>
  • 写回答

3条回答 默认 最新

  • dragon8997474 2013-05-01 04:08
    关注

    Your php starting and closing tags are missing -

    function html_form()
    {
    

    Do it --

    <?php
    
    function html_form()
    {
    

    HTML is not recognizing these php methods, do it like the way you did earlier in your code.

    You need to separate logic part with the view, do it something like the way mentioned below(numerous other ways too for doing it)

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

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据