Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带有一个PHP脚本的多个下拉菜单_Php_Sql Server_Forms - Fatal编程技术网

带有一个PHP脚本的多个下拉菜单

带有一个PHP脚本的多个下拉菜单,php,sql-server,forms,Php,Sql Server,Forms,该网站有多个带有表单的页面。其中很多都有下拉菜单。我想写一个PHP脚本来填充多个下拉菜单。我包括了到目前为止我掌握的代码,但我认为我没有走上正确的轨道 order.php <?php include 'functionsFile.php'; ?> <form method="post" action="order.php"> <select name="order_status" id="order_status"> &l

该网站有多个带有表单的页面。其中很多都有下拉菜单。我想写一个PHP脚本来填充多个下拉菜单。我包括了到目前为止我掌握的代码,但我认为我没有走上正确的轨道

order.php

<?php 
    include 'functionsFile.php'; 
?>

<form method="post" action="order.php">
    <select name="order_status" id="order_status">
        <?php foreach ($data as $row): ?>
        <option><?=$row["order_status"]?></option>
        <?php endforeach ?>
    </select>
    <!-- 3 More Drop Menus -->
</form>
<?php
    $table = array('order_status', 'customer', 'warehouse_id', 'order_description');
    fillForm($conn, $table);
?>

functionsFile.php

<?php
    require 'databaseConnection.php';

 function fillForm($conn, $table)
 {
    foreach ($table as $menu)
    {
        $query = 'SELECT * FROM $table';
        $smt = $conn->prepare($query);
        $smt->execute();
        $data = $smt->fetchAll();
    }
}

我可以在您的代码中看到两个错误

试试这个:

function fillForm($conn, $table)
 {
    foreach ($table as $menu)
    {
        $query = 'SELECT * FROM' .$menu;
        $smt = $conn->prepare($query);
        $smt->execute();
        $data = $smt->fetchAll();
    }
}

我在你的代码中看到了一些错误,希望这些修复能帮助你解决。首先,您的
functionsFile.php
文件在尝试使用它生成的数据后被调用。这需要在尝试使用
$data
变量之前进行。其次,您的
fillForm()
函数不返回值。相反,您正在创建一个不能从函数外部访问的作用域变量。尝试像这样重写文件
functionsFile.php

<?php
    require_once ('databaseConnection.php');

    // No need to pass in connection information if this file is the one loading it and it's not scoped.
    function fillForm($table)
    {
        $query = "SELECT * FROM $table";
        $smt = $conn->prepare($query);
        $smt->execute();
        $rows = $smt->fetchAll();

        return $rows;
    }
这将允许您通过只传入表名,尽可能地重用
fillForm()
函数,还将使您能够更好地控制数据在每个表上的显示方式

关于你的其他问题

  • 如果要选择所有内容,则不必传入字段名。如果您不介意抓取所有列,这将允许您访问
    fillForm()
    函数之外所需的内容。至于命名约定,我不会将列命名为与表相同的名称。相反,我会给这个表起一个宽泛的复数名,我会给这个列起一个特定的名称,以表示该列存储的内容。例如表->客户|列1->Id |列2->名称

  • 我不知道该怎么回答这个问题。在你真正使用它之前,确保你理解了你正在写的所有东西。您是指函数的输出,还是php的html输出

  • <?php
        require_once ('functionsFile.php');
    ?>
    
    <form method="post" action=""><!-- You can leave the action blank if it is posting to the same file. -->
        <select name="order_status" id="order_status">
        <?php
            $rows = fillForm('order_status');
    
            foreach ($rows as $row)
            {
                echo '<option value="' . $row['order_status'] . '">' . $row['order_status'] . '</option>';
            }
        ?>
        </select>
        <select name="customer" id="customer">
        <?php
            $rows = fillForm('customer');
    
            foreach ($rows as $row)
            {
                echo '<option value="' . $row['customer'] . '">' . $row['customer'] . '</option>';
            }
        ?>
        </select>
        <!-- Next groups, etc. -->
    </form>