ronaldb103 Posted March 8, 2010 Share Posted March 8, 2010 I have a form and the php is inside the HTML but I don't think the formatting is correct I have some HTML at the start a form then PHP and then combination of PHP and HTML to display the results. There are two place where I check for empty field or valid results then echo something and use the exit command. I think this is causing me problems because this is not letting the close body and close html tags to be generated. Here is the code below: <html> <body> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>Eugene Pioneer Cemetery</title> <script type="text/javascript" src="js/nav_buttons.js"></script> <LINK rel="stylesheet" type="text/css" href="css/ds.css"> </head> <body bgcolor="#EEEEEE"> <table align="center" cellpadding="0" cellspacing="0" width="891" border="0"><tr><td align="center" class="form" valign="top">WHOLE BUNCH OF HTML<img src="img/TP_pixel.gif" width="1" height="10" border="0" alt=""><form action="record.php" method="post"> <div align="left"><img src="img/TP_pixel.gif" width="280" height="1" border="0" alt="">First Name:<img src="img/TP_pixel.gif" width="105" height="1" border="0" alt="">Last Name:</div><input type="text" name="first" /><img src="img/TP_pixel.gif" width="20" height="1" border="0" alt=""><input type="text" name="last" /><br /> <img src="img/TP_pixel.gif" width="1" height="30" border="0" alt=""><br> <input type="submit" name="submit" value="Submit" /> </form> <?php mysql_connect("antondad.db.4161444.hostedresource.com",$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $first = $_POST['first']; $last = $_POST['last']; $query="SELECT * FROM names where last like '%$last%' AND first like '%$first%'"; if ($last == "") { echo "<p>Please enter a last name</p>"; exit; } $result=mysql_query($query); $num=mysql_numrows($result); if(mysql_num_rows($result)==0){ echo "<p>Sorry no results found</p>"; exit; } mysql_close(); ?> </td></tr> <TR><TD align="center"> <table border="1" cellspacing="2" cellpadding="2" align="center"> <tr> <th><font face="Arial, Helvetica, sans-serif">Block</font></th> <th><font face="Arial, Helvetica, sans-serif">Plot</font></th> <th><font face="Arial, Helvetica, sans-serif">First Name</font></th> <th><font face="Arial, Helvetica, sans-serif">Last Name</font></th> </tr> <?php $i=0; while ($i < $num) { $f1=mysql_result($result,$i,"block"); $f2=mysql_result($result,$i,"plot"); $f3=mysql_result($result,$i,"first"); $f5=mysql_result($result,$i,"last"); ?> <tr> <td><class="body"><?php echo $f1; ?></font></td> <td><font face="Arial, Helvetica, sans-serif"><?php echo $f2; ?></font></td> <td><font face="Arial, Helvetica, sans-serif"><?php echo $f3; ?></font></td> <td><font face="Arial, Helvetica, sans-serif"><?php echo $f5; ?></font></td> </tr> <?php $i++; } ?> </TABLE></td></tr></table> </body> </html> Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 8, 2010 Share Posted March 8, 2010 You should never be using die() when there are validation errors. You should separate the logic from the presentation in your code. Do all the PHP code up front and THEN create the output. The only PHP code that should be interspersed in the HTML is simple echo commands for output text you generate in the logic. Here is a rewrite of your page. See if this works for you. Notice the separatation of the logic at the top of the page and the output at the bottom When following this process you can separate the logic and the output into separate files. Make the managemetn and upkeep much simpler. NOTE: I did not test this, so there may be some syntax errors. <?php $error_msg = ''; $fname = ''; $lname = ''; if (isset($_POST['first']) || isset($_POST['last'])) { //User submitted a search query $fname = trim($_POST['first']); $lname = trim($_POST['last']); if(empty($lname)) { //No last name entered $validationError = "Please enter a last name"; } else { //Perform the database search mysql_connect("antondad.db.4161444.hostedresource.com", $username, $password); @mysql_select_db($database) or die( "Unable to select database"); $sql_fname = mysql_real_escape_string($fname); $sql_lname = mysql_real_escape_string($lname); $query="SELECT block, plot, first, last FROM names WHERE last LIKE '%{$sql_lname}%' AND first LIKE '%{$sql_fname}%'"; $result=mysql_query($query); if(!$result) { $error_msg = "Database error occured."; } elseif(mysql_num_rows($result)==0) { $error_msg = "Sorry, no results found."; } else { $recordResults = ''; while ($record = mysql_fetch_assoc($result)) { $recordResults .= " <tr>\n"; $recordResults .= " <td><class=\"body\">{$record['block']}</font></td>\n"; $recordResults .= " <td><font face=\"Arial, Helvetica, sans-serif\">{$record['plot']}</font></td>\n"; $recordResults .= " <td><font face=\"Arial, Helvetica, sans-serif\">{$record['first']}</font></td>\n"; $recordResults .= " <td><font face=\"Arial, Helvetica, sans-serif\">{$record['last']}</font></td>\n"; $recordResults .= " </tr>\n"; } } mysql_close(); } } ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>Eugene Pioneer Cemetery</title> <script type="text/javascript" src="js/nav_buttons.js"></script> <LINK rel="stylesheet" type="text/css" href="css/ds.css"> </head> <body bgcolor="#EEEEEE"> <table align="center" cellpadding="0" cellspacing="0" width="891" border="0"> <tr> <td align="center" class="form" valign="top"> WHOLE BUNCH OF HTML<img src="img/TP_pixel.gif" width="1" height="10" border="0" alt=""> <div style="color:#ff0000;"> <p style="color:#ff0000;"><?php echo $error_msg; ?></p> <form action="record.php" method="post"> <div align="left"> <img src="img/TP_pixel.gif" width="280" height="1" border="0" alt=""> First Name:<img src="img/TP_pixel.gif" width="105" height="1" border="0" alt="">Last Name: </div> <input type="text" name="first" value="<?php echo $fname; ?>" /> <img src="img/TP_pixel.gif" width="20" height="1" border="0" alt=""> <input type="text" name="last" value="<?php echo $lname; ?>" /><br /> <img src="img/TP_pixel.gif" width="1" height="30" border="0" alt=""><br> <input type="submit" name="submit" value="Submit" /> </form> </td> </tr> <tr> <td align="center"> <table border="1" cellspacing="2" cellpadding="2" align="center"> <tr> <th><font face="Arial, Helvetica, sans-serif">Block</font></th> <th><font face="Arial, Helvetica, sans-serif">Plot</font></th> <th><font face="Arial, Helvetica, sans-serif">First Name</font></th> <th><font face="Arial, Helvetica, sans-serif">Last Name</font></th> </tr> <?php echo $recordResults; ?> </table> </td> </tr> </table> </body> </html> Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 9, 2010 Share Posted March 9, 2010 MIght I also suggest that for the query you only match the beginning of first name or last name instead of anywhere in the string? This is typical when doing name searches. For example, I did a seach for last name with the letter "D" expecting all the results where the last name began with "D". Instead I got results for all records where "D" was included anywhere in the last name, such as "Ward". Quote Link to comment Share on other sites More sharing options...
ronaldb103 Posted March 9, 2010 Author Share Posted March 9, 2010 Thanks will try both of those suggestions Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.