0

我希望获得一个从表格生成编辑表单的功能。我想将主键转换为隐藏输入,其余转换为文本框。这是我所能得到的

function getform($table)  {
    $html= "";  
    $result = mysql_query("Select * From $table");
    if (!$result) {
       echo 'Could not run query: ' . mysql_error();
       // return from function here. don't know what value you need.
       return "";
    }
    $fieldnames = array();
    $fieldCount = mysql_num_fields($result);
    for( $i=0; $i<$fieldCount; $i++ ) {
       $fieldnames[] = ucfirst(mysql_field_name( $result , $i ));
    }

);
}

我不知道如何获取主键,其次是字段的大小

4

2 回答 2

0

使用“DESCRIBE table”(其中 'table' 是您的 db 表的名称)来获取您的表的描述,这将为您提供一个如下所示的数组。遍历这个数组(在我的例子中是用户表)将为您提供您需要的有关该表的所有信息。

Array
(
    [user] => Array
        (
            [0] => Array
                (
                    [Field] => id
                    [Type] => int(11)
                    [Null] => NO
                    [Key] => PRI
                    [Default] => 
                    [Extra] => auto_increment
                )

            [1] => Array
                (
                    [Field] => created
                    [Type] => int(11)
                    [Null] => NO
                    [Key] => 
                    [Default] => 
                    [Extra] => 
                )

            [2] => Array
                (
                    [Field] => updated
                    [Type] => int(11)
                    [Null] => NO
                    [Key] => 
                    [Default] => 
                    [Extra] => 
                )

            [3] => Array
                (
                    [Field] => enabled
                    [Type] => int(11)
                    [Null] => NO
                    [Key] => 
                    [Default] => 
                    [Extra] => 
                )

            [4] => Array
                (
                    [Field] => email
                    [Type] => varchar(255)
                    [Null] => YES
                    [Key] => 
                    [Default] => 
                    [Extra] => 
                )

            [5] => Array
                (
                    [Field] => password
                    [Type] => varchar(255)
                    [Null] => YES
                    [Key] => 
                    [Default] => 
                    [Extra] => 
                )

            [6] => Array
                (
                    [Field] => username
                    [Type] => varchar(255)
                    [Null] => YES
                    [Key] => 
                    [Default] => 
                    [Extra] => 
                )

            [7] => Array
                (
                    [Field] => name
                    [Type] => varchar(255)
                    [Null] => YES
                    [Key] => 
                    [Default] => 
                    [Extra] => 
                )

            [8] => Array
                (
                    [Field] => surname
                    [Type] => varchar(255)
                    [Null] => YES
                    [Key] => 
                    [Default] => 
                    [Extra] => 
                )

        )

)
于 2013-08-15T11:14:57.673 回答
0

假设您正在维护一个旧应用程序,请使用您开始使用的内容,而不是告诉您重新开始。

但是,如果这是一个新应用程序,您真的应该花时间学习 mysqli 或 PDO。

function getform($table)  {
    $html= "";  
    $result = mysql_query("Select * From $table");
    if (!$result) {
       echo 'Could not run query: ' . mysql_error();
       // return from function here. don't know what value you need.
       return "";
    }

    $fieldnames = array();
    for( $i=0; $i < mysql_num_fields($result); $i++ ) {
       $t = array();
       $t['name'] = mysql_field_name( $result , $i );
       $t['type'] = mysql_field_type( $result , $i );
       $t['len'] = mysql_field_len( $result, $i );
       $t['flags'] = mysql_field_flags( $result, $i ); 

       $fieldnames[] = $t;
    }
}

这应该产生类似的东西:

Array
(
    [0] => Array
        (
            [name] => ID
            [type] => int
            [len] => 5
            [flags] => not_null primary_key unique_key multiple_key unsigned auto_increment
        )

    [1] => Array
        (
            [name] => CallID
            [type] => int
            [len] => 7
            [flags] => multiple_key unsigned
        )

    [2] => Array
        (
            [name] => Complaint
            [type] => string
            [len] => 255
            [flags] => multiple_key
        )

    [3] => Array
        (
            [name] => Date
            [type] => date
            [len] => 10
            [flags] => multiple_key binary
        )
于 2013-08-15T11:41:41.167 回答