使用php从数据库填充html页面上的下拉菜单

时间:2014-11-05 09:41:15

标签: php html mysql mysqli

我正在尝试从数据库填充下拉菜单,但是当我运行代码时,我没有收到任何错误,但输出显示了我的代码的一部分没有正确的结果。

index.html页面上的代码是

<section id="services" class="emerald">
    <div class="container">
        <div class="row">
            <div class="row">
                <div class="col-md-4 col-sm-6">
                    <div class="media">
                        <div class="media-body">
                            <?php
                                $servername = "localhost";
                                $username = "username";
                                $password = "pwd";
                                $dbname = "db";

                                // Create connection
                                $con = mysqli_connect($servername, $username, $password, $dbname);
                                // Check connection
                                if (!$con) {
                                    die("Connection failed: " . mysqli_connect_error());
                                }

                                $sql = "SELECT treatment_type FROM treatment_type";
                                $result = $con->query($sql);
                                echo "<label for='treatment_type'>Treatment Type: </label>";
                                echo "<select name='treatment_type' id='treatment_type' class='form-control'>";
                                while($row = $result->fetch_assoc()) {
                                echo "<option value='" . $row['treatment_type'] . "'>" . $row['treatment_type'] . "</option>";
                                }
                                echo "</select>";
                            ?>
                        </div>
                    </div>
                </div>
            </div>  
        </div>
    </div>
</section>

我与该框相处的输出为query($sql); echo "Treatment Type: "; echo ",下拉菜单应显示的位置显示".$row['treatment_type']."

2 个答案:

答案 0 :(得分:0)

然后,正如我在评论中提到的那样:&#34;但是输出显示了我的部分代码没有正确的结果&#34;是不是意味着,你看到你的代码?然后你需要将.html替换为.php或将你的配置设置为解析.html为php。

这适用于此问题后面的用户。

答案 1 :(得分:0)

首先,您应该将文件类型更改为.php

然后当kern说你应该查看MVC框架工作,现在我要做的是将连接移动到类中的函数

这个例子是使用ODBC连接,但是点相同只是将它改为mysqli或者查看使用PDO

public static function functionName($variableToPassToQuery) {

   //Change this part so you are connecting to your database       

    $conn = odbc_connect('SomeDatabase', '', '');
    if (!$conn) {
        exit("Connection Failed: " . $conn);
    }

   // Change to the SQL you need

    $sql = "SELECT * FROM Somewhere WHERE Something = $VariableToPassToQuery)";

    $rs = odbc_exec($conn, $sql);
    if (!$rs) {
        exit("Error in SQL");
    }

   //then once the query has been run we need to get the results and put them in to an array          

    while ($data = odbc_fetch_array($rs)) {
        $row[] = $data;
    }

   //And then return that array 
    return $row;
}

将所有内容放入课堂

现在在你应该改成PHP文件的Html文件中我们可以像这样做下拉框

所以在表格中你需要告诉它我们需要一个下拉框

    <select name="dropdown">
        <?php
         //then for each of the results in the array we need to an an option
         //call the function from the class like this Classname::Functionname($variable) of course if you
         //don't need to pass a variable then dont
        foreach (Classname::functionName($variableToPassToQuery) as $a) {
            echo '<option value=' . $a["SecondRef"] . '>' . $a["Description"] . ' </option>';
        }
        ?>
    </select> 
相关问题