使用PHP填充HTML选择

问题描述:

My goal is to populate a html select box from mysql using a php function. I started by putting the code directly on the html page, and I got it working.

<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>";
?>

I have multiple select boxes, so I thought it would be easier to create a function and pass the table name, and field name as arguments

I moved the php function to its own file, and use an include statement in my html. But once I tried to call the PHP function, the select box wont populate. The select box shows up on the form like it should, but its empty.

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 Code

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

Where did I go wrong when I called the function?

Is it better practice to use a function or am I better off just writing separate code for each select box?

*This is my first time posting, so I apologize if my post is not correct or formatted poorly.

The problem is that your $link variable is not defined. You need to create a class, OOP makes life easier, like this:

//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;
    }
}

Then you can call this on your other pages by including that .php file and accessing the class, like this:

//your some_html.php

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

It looks like your $link object is not available in the function you declared. Try passing the $link object to the function.

It could look something like this:

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

You could also pass the connection as a reference to the function.

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

And then when you call it, add $link as the third argument. Otherwise, you're looking for a global variable inside of a function.