如何在SQL的下拉列表中显示单个表



我在网站的一个选项卡上有5个下拉菜单。我在MS SQL Server中有一个数据库表。该表具有5个下拉列表的所有数据,其中一个名为region_name的字段名称,例如a,b,c,d和E。现在,我想知道我是否可以修改相同的代码以显示关联的表,并在单击下拉列表时使用不同的查询启用行编辑。这可以减少代码重复并改善性能。但是我不知道如何实现这一目标。有人可以给我一些提示吗?我正在使用PHP PDO连接到数据库。

我显示表的代码如下所示。我正在使用DataTables插件和TableExport jQuery插件。为简单起见,我尚未复制插件的链接和库。如果有帮助,我可以显示我的edit.php。

<?php
 require_once('include/database.php');
 ?>

<!DOCTYPE html>
 <html>
 <head>
<title>Matrix</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
<meta http-equiv="content-type" content="text/html" charset="UTF-8">
<script src="//code.jquery.com/jquery-1.12.3.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
 <body>

<header>

</header> <br/>
<div id="container">
<table id="myTable" class="display" cellpadding="0px" cellspacing="0px"      
 width="100%">
    <thead>
    <tr>
        <th style="width: 1px">ID</th>
        <th style="width: 1px">Site</th>
        <th style="width: 1px">Location</th>
        <th style="width: 1px">Remarks</th>
        <th width="1px">Action</th>
    </tr>
    </thead>

    <tbody>
    <?php
    $stmt = $conn->prepare("SELECT * FROM MATRIX WHERE    
    Region_Name='Charlotte'");
    $stmt ->execute();
    $result = $stmt->fetchAll();
    foreach($result as $row) {
        ?>
        <tr>
            <td><?=$row['ID'];?></td>
            <td><?= $row['Site']; ?></td>
            <td><?= $row['Location']; ?></td>
            <td><?= $row['Remarks']; ?></td>
            <td><a href="edit.php?id=<?= $row['ID']; ?>"> Edit</a></td>

        </tr>

        <?php
    }
    ?>
    </tbody>
</table>
<br/>

 </div>

</body>
</html>

您可以使用SELECT table_name FROM information_schema.tables WHERE table_schema = '<your database name>'获取数据库中的所有表。

您还可以使用此查询获得所有表的列

select * from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME='<your table name>'

最新更新