dongzanghui4624 2017-06-07 08:55
浏览 35
已采纳

从SQL搜索表并在页面上显示其数据

I've been trying to find an easy way for this. A search (dropdown menu) of all tables in mysql, and show their content when I click the table I want to show on the page. Instead of showing just every table on the page I thought it can be easier? Any help would be appreciated!
My code so far:

<?php
$host    = "localhost";
$user    = "heijsdb_user";
$pass    = "maus";
$db_name = "heijsdb";

//create connection
$connection = mysqli_connect($host, $user, $pass, $db_name);

//test if connection failed
if(mysqli_connect_errno()){
    die("connection failed: "
        . mysqli_connect_error()
        . " (" . mysqli_connect_errno()
        . ")");
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
echo "borsten HFP controle";
$result = mysqli_query($connection,"SELECT * FROM borstenHFPcontrole");
$all_property = array();  //declare an array for saving property

//showing property
echo '<table class="data-table w3-table-all" border="2px">
        <tr class="data-heading">';  //initialize table tag
while ($property = mysqli_fetch_field($result)) {
    echo '<td>' . $property->name . '</td>';  //get field name for header
    array_push($all_property, $property->name);  //save those to array
}
echo '</tr>'; //end tr tag

//showing all data
while ($row = mysqli_fetch_array($result)) {
    echo "<tr>";
    foreach ($all_property as $item) {
        echo '<td>' . $row[$item] . '</td>'; //get items using property value
    }
    echo '</tr>';
}
echo "</table>";
////////////////////////////////////////////////////////////////////////////////////////////////////////////
  • 写回答

1条回答 默认 最新

  • doubi4340 2017-06-07 23:24
    关注

    This is pretty much the idea, you can play from here and adapt it to your solution. Sorry I used my way, I prefer PHP template style when embedding in HTML. ;)

    $host    = "localhost";
    $user    = "heijsdb_user";
    $pass    = "maus";
    $db_name = "heijsdb";
    
    //create connection
    $connection = mysqli_connect($host, $user, $pass, $db_name);
    
    //test if connection failed
    if(mysqli_connect_errno()){
        die("connection failed: "
            . mysqli_connect_error()
            . " (" . mysqli_connect_errno()
            . ")");
    }
    
    //check if the form was submitted
    $table = filter_input(INPUT_POST, 'table', FILTER_SANITIZE_STRING);
    
    ?>
    <html>
    <head>
        <title>showing table content on user action</title>
    </head>
    <body>
        <div>
    
            <form id="form-menu" method="post">
                <label for="select-menu">Choose a table</label>
                <select id="select-menu" name="table">
                    <option></option>
                    <?php
                    $result = mysqli_query($connection,"SELECT table_name FROM information_schema.tables where table_schema='test'"); // <-- the table_schema field here is your database name, change 'test' for yours
                    while ($row = mysqli_fetch_array($result)) : $selected = $row['table_name'] == $table ? 'selected' : ''; ?>
                        <option value="<?php echo $row['table_name'] ; ?>" <?php echo $selected; ?>><?php echo $row['table_name'] ; ?></option>
                    <?php endwhile; ?>
                </select>
            </form>
    
            <hr>
    
            <div>
                <?php if (empty($table)) : ?>
                    <h3>Please select a table to show its content</h3>
                <?php else : ?>
                    <h3>Content for the table `<?php echo $table; ?>`</h3>
                    <?php
                    $result = mysqli_query($connection,"SELECT * FROM `{$table}`");
                    $all_property = [];  //declare an array for saving property
                    ?>
                    <!-- showing property -->
                    <table class="data-table w3-table-all" border="2px">
                        <tr class="data-heading"> <!-- initialize table tag -->
                            <?php while ($property = mysqli_fetch_field($result)) : ?>
                            <td><?php echo $property->name; ?></td> <!-- get field name for header -->
                            <?php $all_property[] = $property->name; //save those to array ?>
                            <?php endwhile; ?>
                        </tr> <!-- end tr tag -->
    
                        <!-- showing all data -->
                        <?php while ($row = mysqli_fetch_array($result)) : ?>
                        <tr>
                            <?php foreach ($all_property as $item) : ?>
                                <td><?php echo $row[$item]; ?></td> <!-- get items using property value -->
                            <?php endforeach; ?>
                        </tr>
                        <?php endwhile; ?>
                    </table>
                <?php endif; ?>
            </div>
    
        </div>
    
        <script>
            document.getElementById('select-menu').addEventListener('change', function() {
                document.getElementById('form-menu').submit();
            });
        </script>
    </body>
    </html>
    

    Handy links:
    - Get table names using SELECT statement in MySQL
    - Examples of how to do query, style, dom, ajax, event etc like jQuery with plain javascript.

    Hope this helps :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab