问题:PHP - 我的下拉数组列表似乎没有显示任何查询结果

我是 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>&nbsp;<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 代码本身是否不正确)。

解答

将您的输入类型从button更改为submit,如下所示:

<input type="submit" name="submit" id="submit" value="View">
Logo

PostgreSQL社区为您提供最前沿的新闻资讯和知识内容

更多推荐