【问题标题】:populate dropdown menu on html page from database using php使用 php 从数据库中填充 html 页面上的下拉菜单
【发布时间】:2015-01-01 10:41:15
【问题描述】:

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

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']."

【问题讨论】:

  • 试试 var_Dump($con-error);
  • 我叶村的朋友鸣人想说的是var_dump($con-&gt;error);
  • @Ares Draguna 你能告诉我正确的使用方法吗
  • 你应该把你的连接脚本放在你的代码顶部的数据库中:当你必须在你的模板的另一个地方访问这个连接时,它会更具可读性(你还应该阅读关于设计模式和 MVC 来学习更好的代码结构)对于剩下的问题,我承认我不完全了解您当前的输出是什么。你是说你的 PHP 代码是在没有被你的服务器处理的情况下打印出来的?
  • "但输出显示我的代码的一部分没有正确的结果" 是说,你看到你的代码了吗?然后你需要将你的 .html 替换为 .php 或将你的配置设置为将 .html 解析为 php。

标签: php html mysql mysqli


【解决方案1】:

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

这适用于稍后回答此问题的用户。

【讨论】:

    【解决方案2】:

    首先您应该将文件类型更改为 .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> 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-04
      • 1970-01-01
      • 2010-10-06
      • 2011-11-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多