twitter-bootstrap Bootstrap 下拉菜单从 MySQL 中选择

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33798643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 23:22:11  来源:igfitidea点击:

Bootstrap Dropdown Select From MySQL

phpmysqltwitter-bootstrapcomboboxhtml-select

提问by vincmeister

Newbie question, what's wrong with my code, i can't populate dropdown select from mysql table. please advise, thank you

新手问题,我的代码有什么问题,我无法从 mysql 表中填充下拉选择。请指教,谢谢

<html>
<head>
    <link rel="stylesheet" type="text/css" href="DataTables/Bootstrap-3.3.5/css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="DataTables/DataTables-1.10.10/css/jquery.dataTables.min.css">
</head>

<body>  
    <div id="wrapper">
        <nav class="navbar navbar-default navbar-static-top" role="navigation" style="margin-bottom: 0">
            <div class="container-fluid">
                <div class="navbar-header">

                </div>
                <div class="collapse navbar-collapse" id="bs-dtb_master_item-navbar-collapse-1">
                    <ul class="nav navbar-nav">
                        <li><a href="index.html">Home</a></li>
                        <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Data <span class="caret"></span></a>
                            <ul class="dropdown-menu">
                                <li><a href="master-item.html">Item</a></li>
                                <li><a href="master-brand.html">Brand</a></li>
                                <li><a href="master-supplier.hatml">Supplier</a></li>
                                <li><a href="master-customer.html">Customer</a></li>
                                <li><a href="master-expedition.html">Expedition</a></li>
                            </ul>
                        </li>
                        <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Transaksi <span class="caret"></span></a>
                            <ul class="dropdown-menu">
                                <li><a href="receiving.html">Receiving</a></li>
                                <li><a href="update-data.html">Update Data</a></li>
                                <li><a href="delivery-order.html">Delivery Order</a></li>
                                <li><a href="packing-slip.html">Packing Slip</a></li>
                            </ul>
                        </li>
                    </ul> 
                </div>
            </div>
        </nav>
    </div>

    <div class="container-fluid">
        <div class="row">
            <div class="col-md-12">
                <div class="row">
                    <div class="col-md-12">
                        <div class="page-header">

                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-12">

                    </div>
                </div>
                <div class="row">
                    <div class="col-md-12">
                        <!-- START TOMBOL ACTION -->
                        <!-- Button trigger modal -->
                        <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal">
                            Add New
                        </button>
                        <br><br>
                        <!-- END TOMBOL ACTION -->

                        <!-- START MODAL -->
                        <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" action="input_item.php" method="post">
                            <div class="modal-dialog modal-lg" role="document">
                                <div class="modal-content">
                                    <div class="modal-header">
                                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                        <h4 class="modal-title" id="myModalLabel">Input Item</h4>
                                    </div>
                                    <div class="modal-body">
                                        <!-- START FORM INPUT -->
                                        <form class="form-horizontal">
                                            <div class="form-group">
                                                <label class="col-sm-3 control-label">Item Code</label>
                                                <div class="col-sm-3">
                                                    <input type="text" class="form-control" placeholder="Item Code" required="required" name="item_code">
                                                </div>
                                            </div>
                                            <div class="form-group">
                                                <label class="col-sm-3 control-label">Item Name</label>
                                                <div class="col-sm-4">
                                                    <input type="text" class="form-control" placeholder="Item Name" required="required" name="item_name">
                                                </div>
                                            </div>
                                            <div class="form-group">
                                                <label class="col-sm-3 control-label">SKU</label>
                                                <div class="col-sm-3">
                                                    <select class="form-control">
                                                        <?php
                                                        #Include the connect.php file
                                                        include ('C:/xampp/htdocs/b/incl/connect.php');
                                                        $mysqli = new mysqli($hostname, $username, $password, $database);
                                                        /* check connection */
                                                        if (mysqli_connect_errno())
                                                            {
                                                            printf("Connect failed: %s\n", mysqli_connect_error());
                                                            exit();
                                                            }
                                                        $query = "SELECT * FROM master_sku";
                                                        $result = mysql_query($query);
                                                        while ($rows = mysql_fetch_array($result)) {
                                                        echo "<option value=" .$rows['sku_code']. ">" .$rows['sku_name']. "</option>";
                                                        }
                                                        ?> 
                                                    </select>
                                                </div>
                                            </div>
                                            <div class="form-group">
                                                <label class="col-sm-3 control-label">Supplier Item Code</label>
                                                <div class="col-sm-4">
                                                    <input type="text" class="form-control" placeholder="Supplier Item Code" name="vendor_item_code">
                                                </div>
                                            </div>
                                            <div class="form-group">
                                                <div class="col-sm-offset-2 col-sm-10">
                                                    <button type="submit" class="btn btn-primary">Submit</button>
                                                </div>
                                            </div>
                                        </form>
                                        <!-- END FORM INPUT -->
                                    </div>
                                </div>
                            </div>
                        </div>


                        <!-- END MODAL -->

                        <!-- START DATA TABEL -->
                        <div class="panel panel-default">
                            <div class="panel-heading">Item Management</div>
                            <div class="panel-body">
                                <table id="dtb_master_item" class="table table-striped" cellspacing="0" width="100%">
                                    <thead>
                                        <tr>
                                            <th>Item Code</th>
                                            <th>Item  Name</th>
                                            <th>SKU</th>
                                            <th>Vendor Item Code</th>
                                        </tr>
                                    </thead>
                                </table>
                            </div>
                        </div>
                        <!-- END DATA TABEL -->
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- CORE JS -->
    <script src="DataTables/jQuery-1.11.3/jquery-1.11.3.min.js"></script>
    <script type="text/javascript" src="js/jquery.min.js"></script>
    <script type="text/javascript" src="DataTables/Bootstrap-3.3.5/js/bootstrap.min.js"></script>

    <!-- DATATABLES JS -->  
    <script type="text/javascript" src="DataTables/DataTables-1.10.10/js/jquery.dataTables.min.js"></script>
    <script type="text/javascript" src="DataTables/DataTables-1.10.10/js/dataTables.bootstrap.min.js"></script>

    <script>
        $(document).ready(function() {

        // START DATATABLES MASTER ITEM 
        $('#dtb_master_item').dataTable( {
        "processing": true,
        "serverSide": true,
        "ajax": "incl/ssp_master_item.php",
         "columns": [
            { "width": "10%" },
            { "width": "50%" },
            { "width": "10%" },
            { "width": "30%" }
          ]
        } );

        // END DATATABLES MASTER ITEM   

        } );
    </script>
</body>

enter image description here

在此处输入图片说明

if i separate my php code on a new php page, the query working fine enter image description here

如果我在一个新的 php 页面上分离我的 php 代码,查询工作正常 在此处输入图片说明

it's working fine with html manual code (not from database)

它与 html 手动代码(不是来自数据库)一起工作正常

        <select class="form-control">
                    <option>1</option>
                    <option>2</option>
                    <option>3</option>
        </select>

enter image description here

在此处输入图片说明

回答by uno

Put '' on your $rows[sku_code]

把 '' 放在你的 $rows[sku_code]

like

喜欢

$rows['sku_code']

$rows['sku_code']

and concatenate your code.

并连接您的代码。

   echo "<option value=" .$rows['sku_code']. ">" .$rows['sku_name']. "</option>";

回答by Rahul

Change your code line

更改您的代码行

echo "<option value="$rows[sku_code]">$rows['sku_name']</option>";

to

echo "<option value=" . $rows['sku_code'] . ">" . $rows['sku_name'] . "</option>";

Now It should work fine.

现在它应该可以正常工作。

回答by vincmeister

I found the problem My file is .HTML I put php inside the HTML

我发现了问题 我的文件是 .HTML 我把 php 放在了 HTML 中

so after googling, i found the solusion to edit httpd.conf by adding:

所以在谷歌搜索之后,我找到了通过添加以下内容来编辑 httpd.conf 的解决方案:

AddType application/x-httpd-php .html .htm  

reference: PHP script not working in HTML file

参考:PHP 脚本在 HTML 文件中不起作用