Syntax Issue

This is what I am wanting to do with the code. It does not perform properly as I get a 500 error.

  1. Have user input a first and last name
  2. Verify that there is a valid input in both of the text boxes
  3. On button press query database
  4. If data is returned display header & table
  5. If no data is returned display a message on screen that no data exists.

I have put together the below syntax, but somewhere syntactically there is an issue, and I can not spot it. Can someone with more experience than I, view this code and help me get it up and running?

<html>
<tbody>  
<form method="POST">
Last Name:<input type="text" name="lastname">
First Name:<input type="text" name="firstname">
<input type="submit" name="submit" value="Get Info">
</tbody>
</html>
</form>
<table border="1">
<thead>
<tr>
<th >Second Answer </th>
<th>First Answer </th>
</tr>
</thead>
<tbody>

<?php
if(isset($_POST['submit'])){
$fnameerror = false;
$lastnameerror = false;
if (empty($_POST['firstname'])) {
$fnameerror = true;
}
if (empty($_POST['lastname'])) {
$lastnameerror = true;
}
if ($fnameerror) {
echo "<strong>Please input the first name.</strong><br>";
} else if ($lastnameerror) {
echo "<strong>Please input a last name.</strong><br>";
} else { 

$option = array();
$option['driver'] = 'mssql'; 
$option['host'] = '192.168.X.XXX'; 
$option['user'] = 'hamin'; 
$option['password'] = 'ha'; 
$option['database'] = 'Test'; 
$option['prefix'] = '';
$db = JDatabase::getInstance( $option );
$query = $db->getQuery(true); } }
?>
<html>
<head>
</head>
<body>
<table border="1">
<thead>
<tr>
<th >Second Answer </th>
<th > First Answer </th>
</tr>
</thead>
</table>
</div>

<div>
<div>
<table border="1" width="960px">
<?php
$query = "Select Top 3 second, first from orderhistory";

$db->setQuery($query); 
$query = $db->loadObjectList();
if($query)
{
if ($query->num_rows > 0) 
{echo"<table>";
foreach( $query as $res ) { 
print "<tr>";
print "<td>" .$res->Second."</td>";
print "<td>" .$res->First."</td>";
print "</tr>";
}
echo"</table>";
}
else {
echo "<h3 align=\"center\">No Data Returned.</h3>";
}
else {
echo "<br> Database error.";
}
?>
</table>
</div>
<br>
</div>
</body>
</html>

500 is an internal server error, check the (web) server logs and you should see an error message which should be helpful

[member=71845]JimL[/member] -
The error shown is “syntax error, unexpected ‘<’, expecting end of file”

I suggest you use a proper IDE, like Netbeans, PHPStorm, or similar as they highlight errors for you. You have some problems with your last if/else blocks and if you work with the code like you pasted it above (missing newlines, no indention, etc) you will have huge problems reading/making sense of it.

[member=71845]JimL[/member] - using NetBeans I have been able to remove majority of the issues, thank you for that tip! I am stuck on this one though…
With this code

foreach( $query as $res ) { 
 print "<tr>";
 print "<td>" .$res->Second."</td>";
 print "<td>" .$res->First."</td>";
 print "</tr>";
 }

NetBeans is telling me unexepcted print after string “

Does that syntax look awry to you?

Wait, I found the issue! Thank you for the words of wisdom, to help me discover my issue!

Sponsor our Newsletter | Privacy Policy | Terms of Service