使用PHP填充HTML选择

时间:2015-02-16 18:36:44

标签: php html mysql

我的目标是使用php函数从mysql填充html选择框。我首先将代码直接放在html页面上,然后让它运行起来。

<label for="product_Category">Product Category</label>
<?
$field_Name = "category_Name";
$table_Name = "Product_Category";

$sql = "Select " . $field_Name . " From " . $table_Name;
$results = mysqli_query($link, $sql);
echo "<select>";
echo "<option value = \" \" >Select Category"</option>;
while($row = mysqli_fetch_array($results, MYSQLI_ASSOC)){
echo "<option value = ' " .$row[$field_Name] . "'>" . $row[$field_Name]. "</option>";
}
echo "</select>";
?>

我有多个选择框,所以我认为创建一个函数并传递表名和字段名作为参数会更容易

我将php函数移动到自己的文件中,并在我的html中使用include语句。但是一旦我尝试调用PHP函数,选择框就不会填充。选择框在表单上显示它应该,但它是空的。

PHP

<? 
function Populate_Select($table_Name, $field_Name){ 

$sql = "Select " . $field_Name . " From " . $table_Name;
$results = mysqli_query($link, $sql);
echo "<select>";
echo "<option value = \" \" >Select Category </option>";
while($row = mysqli_fetch_array($results, MYSQLI_ASSOC)){
    echo "<option value = ' " .$row[$field_Name] . "'>" . $row[$field_Name]. "    </option>";
}
echo "</select>";
}
?>

DB Config

<?
$host = "localhost";
$db_userName = "root";
$db_Password = "root";
$db_Name = "mydb";

$link = mysqli_connect($host, $db_userName, $db_Password, $db_Name);
     if (!$link){
        die("Database Connection failed " . mysqli_connect_error);
     }

?>

HTML代码

<? include 'PopulateSelect.php' ?>
<? include 'DB_Config.php ?>
<!--Rest of HTML CODE-->
<label for="product_Category">Product Category</label>              
<? Populate_Select('Product_Category', 'category_Name'); ?>

当我打电话给这个功能时,我哪里出错了?

使用函数是更好的做法还是我最好只为每个选择框编写单独的代码?

*这是我第一次发帖,所以如果我的帖子不正确或格式不正确,我会道歉。

3 个答案:

答案 0 :(得分:0)

您声明的函数中似乎没有$link对象。尝试将$link对象传递给函数。

看起来像这样:

function Populate_Select($link, $field_Name, $table_Name){ ... }

答案 1 :(得分:0)

问题是您的$link变量未定义。你需要创建一个类,OOP让生活更轻松,就像这样:

//myclass.php

class MyClass {

    private $link;

    function __construct() {
        $host = "localhost";
        $db_userName = "root";
        $db_Password = "root";
        $db_Name = "mydb";
        $this->link = mysqli_connect($host, $db_userName, $db_Password, $db_Name);
        if (!$this->link) {
            die("Database Connection failed " . mysqli_connect_error);
        }
    }

    function __destruct() {
        mysqli_close($this->link);
    }

    function Populate_Select($table_Name, $field_Name) {
        $sql = "Select $field_Name From $table_Name";
        $results = mysqli_query($this->link, $sql);
        $select = "<select>";
        $select .= "<option value=''>Select Category</option>";
        while ($row = mysqli_fetch_array($results, MYSQLI_ASSOC)) {
            $select .= "<option value='$row[$field_Name]'>$row[$field_Name]</option>";
        }
        $select .= "</select>";
        return $select;
    }
}

然后你可以通过包含.php文件和访问类来在其他页面上调用它,如下所示:

//your some_html.php

include 'myclass.php';
$obj = new MyClass();
$select = $obj->Populate_Select("your_table_name", "your_field_name");
echo $select;

答案 2 :(得分:0)

您也可以将连接作为对函数的引用传递。

function Populate_Select($field_Name, $table_Name, &$link){ ... }

然后当你调用它时,添加$ link作为第三个参数。否则,您将在函数内部查找全局变量。