I have different tables like table 1 to 5 in mysql database. I want a code for displaying each table when i select it from drop down table list. when i try to execute the below i got no response and no data loaded form the sql.
<div>
<form action="table1.php" method="GET">
<input list="name" name="name">
<datalist id="name">
<option value="table 1">
<option value="table 2">
<option value="table 3">
<option value="table 4">
<option value="table 5">
</datalist>
<input type="submit" name="search" value="search">
</form>
</div>
<div>
<table width="600" border="0" cellpadding="1" cellspacing="1">
<tr>
<th>Column 1</th>
<th>Column 2</th>
<th>Column 3</th>
<th>Column 4</th>
</tr>
</table>
in the php code i assign a variable for the selection value and given in the program. but i got a error that the variable is undefined or not be used in the $conn statement.
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "sheet";
$tbname= $_GET['name'];
if (isset($_GET['search'])) {
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("SELECT * FROM '$tbname'");
$stmt->execute();
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
echo "<tr>";
echo "<th>".$result['Column 1']."</th>";
echo "<th>".$result['Column 2']."</th>";
echo "<th>".$result['Column 3']."</th>";
echo "<th>".$result['Column 4']."</th>";
echo "</tr>";
}
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
$conn = null;
echo "</table>";
}
"SELECT * FROM $tbname"you don't quote table names.