简体   繁体   English

使用带有html格式的sql表中的sql表中的数据

[英]Use data from sql table in a html form with php

I'm trying to create a form in html which has a combobox with a list that's automatic created from my SQL table. 我正在尝试使用html创建一个表单,该表单具有一个组合框,该组合框具有从SQL表自动创建的列表。 I've managed to make an integrated combobox in my class where I use all my database statements. 我设法在我的课堂上制作了一个集成的组合框,在其中使用了所有数据库语句。 But how do I use the code on another location where my form is written? 但是,如何在编写表单的其他位置使用代码? I'm still learning php and this is kinda new for me so I hope my question is clear to understand. 我仍在学习php,这对我来说是一个新事物,所以希望我的问题清楚易懂。

Code: 码:

    class Lesdb
    {
        private static $lesdbInstantie = null;

        private $dbh;

        private function __construct($server, $username, $password, $database)
        {
            try
            {
                $this->dbh = new PDO("mysql:host=$server; dbname=$database", $username, $password);
                //Bij error: exception opwerpen
                $this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            }
            catch (PDOException $e)
            {
                die($e->getMessage());
            }
        }

        public static function getLesdbInstantie($server, $username, $password, $database)
        {
            if(is_null(self::$lesdbInstantie))
            {
                self::$lesdbInstantie = new Lesdb($server, $username, $password, $database);
            }
            return self::$lesdbInstantie;
        }
    public function testComboBox()
    {
        echo "<h1> ComboBox functienamen </h1>";

            $sql = "SELECT DISTINCT ftienaam FROM werknemers";
            $stmt = $this->dbh->prepare($sql);
            $stmt->execute();

        $dropdown = "<select name='ftienaam'>";
        foreach ($stmt as $row)
        {
            $dropdown .= "\r\n<option value='{$row['ftienaam']}'>{$row['ftienaam']}</option>";
        }
        $dropdown .= "\r\n</select>";

        echo $dropdown;

    }
}

?>

The function TestCombobox does work but it should be implemented into my form which isn't located in the class Lesdb. TestCombobox函数可以正常工作,但应将其实现为不在Lesdb类中的我的表单。 example : 例如:

function ShowForm()
{
    ?>
    <form action="Index.php?actie=zoekInLijst" method=post>
        <label for=ComboBox>ComboBox</label>
            <select name = "the_name">
                <option value="name">SQL STATEMENTS
            </select>
    </form>
<?php
}

So I simply want to call the function testComboBox from the class Lesdb in my form. 因此,我只想从窗体中的Lesdb类调用函数testComboBox。

Assuming your class is in one file, let's call it Lesdb.php and your form is in another file, eg Form.php, then you have to include your class in the form file (right at the top, before the other code), 假设您的课程在一个文件中,我们将其命名为Lesdb.php,而您的表单在另一个文件中,例如Form.php,那么您必须将您的课程包含在表单文件中(在顶部,在其他代码之前),

include_once "<PATH_TO_YOUR_FILE>\Lesdb.php";

create an instance of your class (where you want to use that instance), 创建您的类的实例(您要在其中使用该实例),

Lesdb db = new Lesdb();

and use the function in your ShowForm function. 并在ShowForm函数中使用该函数。

db->testComboBox();

You have to make an new instance of the class. 您必须创建该类的新实例。 Then on that instance you have to load the function. 然后,在该实例上,您必须加载该函数。

Don't forget to include the class in top of the file. 不要忘记在文件顶部包含该类。

$theLesDB = new Lesdb();

$theLesDB->testComboBox();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM