I am creating a page that prompts for server login info, and redirects after submitting correct information. Once on the next page, there is a select form with different table names representing tables inside of the database. Once one is selected, and 'select table' is clicked, it is to display the entire table from the database as a table in html by using PHP. I'm not sure how to figure out the number of columns in a sql table to be able to display it using a loop and html table tags.
<?
session_start();
if(!isset($_SESSION['session']))
{
$_SESSION['session'] = 0;
}
if(isset($_POST['host']))
{
$_SESSION['host'] = $_POST['host'];
$_SESSION['dbname'] = $_POST['dbname'];
$_SESSION['username'] = $_POST['username'];
$_SESSION['pw'] = $_POST['pw'];
}
?>
<!DOCTYPE html>
<html>
<body>
<?
if(isset($_POST['logout']))
{
$_SESSION['session'] = 0;
}
if(isset($_POST['submit']))
{
try
{
$db = new PDO("mysql:host=".$_POST['host'].";dbname=".$_POST['dbname'], $_POST['username'], $_POST['pw']);
}
catch(Exception $error)
{
die("Connection to user database failed: " . $error->getMessage());
}
try
{
$db->setAttribute(PDO::ATTR_ERRMODE, PDO:: ERRMODE_EXCEPTION);
$query = "SHOW TABLES";
$results = $db->query($query)->fetchall();
$_SESSION['session']=1;
}
catch(Exception $error)
{
echo "Problem with ~query, go back to home screen";
$_SESSION['session']=0;
}
}
if($_SESSION['session'] == 0)
{?>
<form action="<?$_SERVER['PHP_SELF']?>" method="post" name='initialentry'>
<table border='0' style='text-align: center'>
<tr>
<td style='text-align: right;'>Enter host name:</td>
<td style='text-align: left;'>
<input type='text' name='host' value='localhost'>
</td>
</tr>
<tr>
<td style='text-align: right;'>Enter database name:</td>
<td style='text-align: left;'>
<input type='text' name='dbname' value='zxyx999'>
</td>
</tr>
<tr>
<td style='text-align: right;'>Enter user name:</td>
<td style='text-align: left;'>
<input type='text' name='username' value='zxyx999'>
</td>
</tr>
<tr>
<td style='text-align: right;'>Enter password:</td>
<td style='text-align: left;'>
<input type='password' name='pw' width='15' value='12345'>
</td>
</tr>
<tr>
<td style='text-align: right;'>
<input type="reset" value="Reset">
</td>
<td style='text-align: left;'>
<input name='submit' type="submit" value="Submit">
</td>
</tr>
</table>
</form>
<?}
if($_SESSION['session']==1)
{
?><form action='<?$_SERVER['PHP_SELF']?>' method='post' name='tableList'>
<select name='selected'><?
foreach($results as $row)
{
echo "<option value=".$row[0].">" . $row[0] . "</option>";
}?>
</select>
<input type="submit" name="selectTable" value="Select Table">
<input type="submit" name="logout" value="logout">
</form><?
}
if(isset($_POST['selectTable']))
{
try
{
$db = new PDO("mysql:host=".$_SESSION['host'].";dbname=".$_SESSION['dbname'], $_SESSION['username'], $_SESSION['pw']);
}
catch(Exception $error)
{
die("Connection to user database failed: " . $error->getMessage());
}
try
{
$statement = $db->prepare("SELECT * FROM ".$_POST['selected']);
$statement->execute();
}
catch(Exception $error)
{
echo "Problem with ~query";
}
echo "</br>";
echo "<table border= '1'>";
while($row = $statement->fetch())
{
echo $row['book_id'] . $row['title'];
}
echo "</table>";
}
?>
</body>
</html>