【问题标题】:call a function with php dropdown使用 php 下拉菜单调用函数
【发布时间】:2021-11-21 18:37:15
【问题描述】:

我有来自 index.php 的这段代码。我的数据库连接是在 index.php 中设置的 & sql 查询也可以。调用该函数后无法正常工作。

<?php
  $connect = new PDO('mysql:host=localhost;dbname=test', 'root', '');
?>
<?php
  include('function.php');
?>
<form class="form-horizontal" name="myform" action="index.php" method="POST">
    <div class="form-group">
        <label class="col-lg-2 control-label">Institute</label>
        <div class="col-lg-6">
            <select class="form-control" name="institute"  id="institute">
            <option class="col-lg-6" value="" selected="selected" >- Select Institute -</option>
            <?php 
              echo fill_institute_list($connect);   
            ?>
            </select>


        </div>
    </div>
</form>

然后调用function.php

function fill_institute_list($connect)
{
    $query = "
    SELECT * FROM cadre_institute
    ORDER BY institute ASC
    ";
    $statement = $connect->prepare($query);
    $statement->execute();
    $result = $statement->fetchAll();

    $output = '';
    foreach($result as $row)
    {
        $output .= '<option value="'.$row["id"].'">'.$row["institute"].'</option>';
    }
    return $output;
}

然后在浏览器中下拉字段后不显示..

【问题讨论】:

  • 然后不工作 对问题的描述没有帮助!您是否检查了日志文件中的错误?
  • PHP 8.0 之前的版本默认不显示 PDO 错误。您需要在连接到数据库后立即tell it otherwise。

标签: php function dropdown


【解决方案1】:

尝试改变这些

        $output .= '<option value="'.$row["id"].'">'.$row["institute"].'</option>';

到

        $output .= '<option value="'.$row[id].'">'.$row[institute].'</option>';

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-10
    • 1970-01-01
    • 2018-07-09
    • 1970-01-01
    • 1970-01-01
    • 2017-06-25
    • 2019-01-17
    • 1970-01-01
    相关资源
    最近更新 更多