我是PHP的新手,我在解决如何修复此代码段时遇到了问题。
<!DOCTYPE html>
<html>
<?php
//Connection details --- Do not delete
require $_SERVER["DOCUMENT_ROOT"]."/connect.php";
?>
<head>
<meta http-equiv="Content-Type" content="text/html">
<meta charset="utf-8">
<title>Table</title>
</head>
<body>
<?php
//This places all table in the array and used as a dropdown list for "Select Table" form
$table_fetch = $dbh->query("SELECT * FROM information_schema.tables WHERE table_schema = 'meta_auto_reports' and table_type = 'BASE TABLE'");
$tables = array();
while ($row = $table_fetch->fetch(PDO::FETCH_ASSOC))
{
$tables[] = $row['table_name'];
}
?>
<h1>Select Table to View</h1>
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="POST">
<select name="table">
<option>Select Table</option>
<?php
foreach ($tables as $table) {
echo "<option value='$table'>" . $table . " </option>";
}
?>
</select> <input type="button" name="submit" id="submit" value="View">
</form>
<?php
if(isset($_POST['submit'])){//if the submit button is clicked
$var_table = $_POST["table"];
$query = "select * FROM meta_auto_reports.".$var_table."";
$data = $dbh->query($query);
while ($row = $data->fetch(PDO::FETCH_ASSOC)){
print_r($row);
}
}
?>
</body>
</html>
下拉列表来自列出架构meta_auto_reports中的所有表,并且它完美地列出了表。但是我的提交按钮没有给出查询结果(不确定结束标记的放置,列表的值和/或php代码本身是不是正确的)。
答案 0 :(得分:2)
将您的输入类型从button
更改为submit
,如下所示:
<input type="submit" name="submit" id="submit" value="View">