Using mysql_fetch_row( ) to generate edit form

时间:2022-09-25 16:17:36

I am hoping to get a function to generate an edit form from a table. I would like to convert the Primary key to a hidden input and the rest to textboxes. This is as far as i could get

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

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

);
}

I cant figure out how to get the primary keys and secondly the size of the feilds

我无法弄清楚如何获得主键,其次是如何获得主要的大小

2 个解决方案

#1


0  

Using what you started with rather than telling you to start again, assuming you are maintaining an old application.

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

However if this is a new app you really shoulld invest the time to learn mysqli or PDO instead.

但是,如果这是一个新的应用程序,你真的应该花时间学习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;
    }
}

This should produce something like:

这应该产生类似的东西:

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
        )

#2


0  

Use "DESCRIBE table" (where 'table' is the name of your db table) to get a description of your table, which will give you an array as follows below. Recursing through this array (user table, in my case) will give you all the information you need about the table.

使用“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] => 
                )

        )

)

#1


0  

Using what you started with rather than telling you to start again, assuming you are maintaining an old application.

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

However if this is a new app you really shoulld invest the time to learn mysqli or PDO instead.

但是,如果这是一个新的应用程序,你真的应该花时间学习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;
    }
}

This should produce something like:

这应该产生类似的东西:

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
        )

#2


0  

Use "DESCRIBE table" (where 'table' is the name of your db table) to get a description of your table, which will give you an array as follows below. Recursing through this array (user table, in my case) will give you all the information you need about the table.

使用“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] => 
                )

        )

)