error in my PHP code drop down list to select item from MYSQL
i have interface which have drop down list, you have to select an item and
click on submit button to view the database in mysql but doesn't work, it
give error "Table 'balhaf.$table' doesn't exist"
here is my code the interface
<html>
<body>
<form method="post" action="list_files.php">
<input name="go" type="submit" value="submit" / >
<?php
$dbname = 'balhaf';
if (!mysql_connect('localhost', 'sqldata', 'sqldata')) {
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;
}
echo '<select name="dropdown" style="width:150px">';
echo '<option value="">Select</option>';
while ($row = mysql_fetch_row($result)) {
echo '<option value="'.$row[0].'">'.$row[0].'</option>';
}
echo '</select>';
echo '</form>';
mysql_free_result($result);
?>
</body>
</html>
my second code "list_files.php"
<?php
if(isset($_POST["dropdown"]))
{
echo "ok";
}
$table = $_POST['dropdown'];
// Connect to the database
$dbLink = new mysqli('localhost', 'sqldata', 'sqldata', 'balhaf');
if(mysqli_connect_errno()) {
die("MySQL connection failed: ". mysqli_connect_error());
}
// Query for a list of all existing files
$sql = 'SELECT `id`, `name`, `mime`, `size`, `created` FROM $table';
$result = $dbLink->query($sql);
// Check if it was successfull
if($result) {
// Make sure there are some files in there
if($result->num_rows == 0) {
echo '<p>There are no files in the database</p>';
}
else {
// Print the top of a table
echo '<table border="1" align="center">
<H2 align="center"> Report Table</H>
<tr>
<td><b>Name</b></td>
<td><b>Mime</b></td>
<td><b>Size (bytes)</b></td>
<td><b>Created</b></td>
<td><b>Download</b></td>
</tr>';
// Print each file
while($row = $result->fetch_assoc()) {
echo "
<tr>
<td>{$row['name']}</td>
<td>{$row['mime']}</td>
<td>{$row['size']}</td>
<td>{$row['created']}</td>
<td><a style='text-decoration:none;'
href='get_file.php?id= {$row['id']}'>Download</a></td>
</tr>";
}
// Close table
echo '</table>';
}
// Free the result
$result->free();
}
else
{
echo 'Error! SQL query failed:';
echo "<pre>{$dbLink->error}</pre>";
}
// Close the mysql connection
$dbLink->close();
?>
No comments:
Post a Comment