I’ve been trying to fix this problem forever now and its bugging me. the code is to display a table selected from the dropbox which works fine..but when i put it in with my layout/template it throws an error & i have no idea why!
Here’s the code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="keywords" content="" >
<meta name="description" content="" >
<meta http-equiv="content-type" content="text/html; charset=utf-8" >
<title>SNYSB Archive</title>
<link href="style.css" rel="stylesheet" type="text/css" media="screen" >
<!-- Location of javascript. -->
<script language="javascript" type="text/javascript" src="swfobject.js" ></script>
</head>
<div id="wrapper">
<div id="header">
<!-- KEEP THIS BIT [ITS FORMATTING] -->
</div>
<!-- end #header -->
<div id="menu">
<ul>
<li><a href="Hpage.php">Home</a></li>
<li><a href="Register.php">Register</a></li>
</ul>
</div>
<!-- end #menu -->
<div id="page">
<div id="page-bgtop">
<div id="page-bgbtm">
<div id="content">
<div class="post">
<div class="post-bgtop">
<div class="post-bgbtm">
<h1 class="title">PUT HEADING HERE!</h1>
<div class="entry">
<p class="Body">
<?php
$dbname = 'snysbarchive';
$conn= mysql_connect('localhost', 'root', 'usbw');
if (!$conn) {
echo 'Could not connect to mysql';
exit;
}
$sql = "SHOW TABLES FROM $dbname";
$result = mysql_query($sql);
if (!$result) {
echo "DB Error, could not list tables\n";
echo 'MySQL Error: ' . mysql_error();
exit;
}
if (mysql_select_db($dbname, $conn))
{
?>
<form method="post" action="new 2.php">
<select name="tables">
<?php
while ($row = mysql_fetch_row($result)) {
?>
<?php
echo '<option value="'.$row[0].'">'.$row[0].'</option>';
}
?>
</select>
<input type="submit" value="Show">
</form>
<?php
//mysql_free_result($result);
if (isset($_POST) && isset($_POST['tables']))
{
$tbl=$_POST['tables'];
//echo $_POST['tables']."<br />";
$query="SELECT * from $tbl";
$res=mysql_query($query);
echo $query;
if ($res)
{
?>
<table border="1">
<?php
while ( $row = mysql_fetch_array($res))
{
echo "<tr>";
echo "<td>".$row[0]."</td>";
echo "<td>".$row[1]."</td>";
echo "<td>".$row[2]."</td>";
echo "<td>".$row[3]."</td>";
echo "</tr>";
} ?>
</table>
<?php
}
}
?>
</div>
</div>
</div>
</div>
<div style="clear: both;"> </div>
</div>
<!-- end #content -->
<div id="sidebar">
<ul>
<li>
<h2>Welcome!</h2>
<p>Welcome to SNYSBs archive!
</p>
</li>
<li>
<h2>SNYSB</h2>
<p>
<a href="Contact.php">Contact Us!</a>
</p>
</li>
</ul>
</div>
<!-- end #sidebar -->
<div style="clear: both;"> </div>
</div>
</div>
</div>
<!-- end #page -->
<div id="footer">
<p>Copyright (c) 2008 Sitename.com. All rights reserved. Design by <a href="http://www.freecsstemplates.org/">Free CSS Templates</a>.</p>
</div>
<!-- end #footer -->
</div>
</body>
</html>
(Just pasted the entire page’s code from the original with my template at the start and bottom)
This is the line it doesnt like:
if (isset($_POST['tables']))
Thanks
Edit: New error message:
Parse error: syntax error, unexpected $end in FILENAME on line 128
The error you’re seeing is likely because $_POST doesn’t have a key called ‘tables’.
both produce errors when I run them. Notice the syntax highlighting color changes on SO, indicating that not all is being interpreted as a string literal. You need to either escape the inner quotes or switch the outer ones to single quotes:
or
The same goes for
Also,
should read
Furthermore, you have an error in your usage of the mysql_* functions. I can see you’re selecting your db only after having run a query, and I don’t see a call to
mysql_connect. The order is connect, select_db, query, fetch, free, close as seen in http://www.php.net/manual/en/mysql.examples-basic.php(After above code was updated)
Wrapping the whole thing in a heredoc is clearly the cause, and is very unnecessary. Simply use php open and closing tags to switch between text and code. For instance instead of
use
Code inside of a heredoc doesn’t get executed (although variables are substituted), so none of the code from
<<< TEMPLATEtoTEMPLATE;is being run. Only variables should go inside a heredoc.