0

我正在尝试使用来自 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>
4

3 回答 3

1

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

替换$partner$type

 $type = $row['type'];

 echo "<option value=\"$type\">$type</option>";
于 2013-11-09T05:50:28.553 回答
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>";
于 2013-11-09T07:30:30.173 回答
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);
            }
        }
于 2013-11-09T05:50:09.143 回答