从数据库填充下拉菜单时出错

时间:2013-11-09 05:45:07

标签: php mysql

我正在尝试使用mysl db中的数据自动填充下拉菜单。下拉列表应显示类型inactiveactive以供用户选择。下面我有来自php端的表结构和sql查询。下拉列表根本没有显示任何值。

CREATE TABLE academy
(
  academy_id int(11) not null auto_increment,
  name varchar(25) not null,
  type enum('INACTIVE','ACTIVE') DEFAULT 'ACTIVE' NOT NULL,
  primary key (id),
 );

PHP

Type: <select name="select_type">
<?php
        $mysqli = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE);

        if (mysqli_connect_errno()) {
            printf("Connect failed: %s\n", mysqli_connect_error());
            exit();
        } 

        $dropDownQuery = "SELECT type FROM academy";

        if ($result = $mysqli->query($dropDownQuery)) {
            while ($row = $result->fetch_row()) {
                $type = $row['type'];
                echo "<option value=\"$type\">$type</option>";
            }
        }
    ?>
</select>

3 个答案:

答案 0 :(得分:1)

$type未在您的代码中定义。

$partner替换为$type

 $type = $row['type'];

 echo "<option value=\"$type\">$type</option>";

答案 1 :(得分:0)

   $dropDownQuery = "SELECT `type` FROM academy";
        if ($result = $mysqli->query($dropDownQuery)) {
            while ($row = $result->fetch_row()) {
                $partner = $row['type'];
                printf ('<option value="%s">%s</option>', $partner, $partner);
            }
        }

答案 2 :(得分:0)

    $mysqli = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE);


   if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    } 

    $table_name = "academy";
    $column_name = "type";

    echo "<select name=\"$column_name\"><option>Select one</option>";
    $q = "SELECT COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS
        WHERE TABLE_NAME = '$table_name' AND COLUMN_NAME = '$column_name'";
    $r = mysqli_query($mysqli, $q);

    $row = mysqli_fetch_array($r);
    //print_r($row);
    $enumList = explode(",", str_replace("'", "", substr($row['COLUMN_TYPE'], 5, (strlen($row['COLUMN_TYPE'])-6))));
    //print_r($enumList);
    foreach($enumList as $value){
        echo "<option value='$value'>$value</option>";
    }
    echo "</select></br>";