tags:

views:

126

answers:

4

Hi friends what is the best way to show the mysql enum values while inserting and updating php page ?

name ENUM('small', 'medium', 'large')

edited: actually I was asking for this . I have a database field

 `color` enum('red','blue','green','white') DEFAULT NULL,

and php form

<label for="color">Colors</label><br />
<input type="text" name="color" />

How do i display enum value on the php form from mysql database? Please help

+1  A: 

Your question is not very clear. If you mean what input method should I use in the form on my php page, the answer below is relevant.

You can use a dropdown:

<select name="name">
    <option value="small">small</option>
    <option value="medium">medium</option>
    <option value="large">large</option>
</select>

Radio buttons are another possibility:

<input type="radio" name="name" value="small"> small
<input type="radio" name="name" value="mediuim"> medium
<input type="radio" name="name" value="large"> large
captaintokyo
+2  A: 

If you have a enum field, the MySQL will return string values for it and you can set with integer and string values too. Simply doing this:

mysql_query("select name from tablename");

will give you full labels like small or medium or large

And you can similarly update them too using full labels like this:

mysql_query("insert into tablename (name) values ('small')");

or numeric values like this:

mysql_query("update tablename set name = 2");
shamittomar
Didn't know this is possible: `update tablename set name = 2` Interesting...
captaintokyo
please have a look i have updated the post –
sagarmatha
+2  A: 

You'd need to do a "SHOW COLUMNS FROM " to get the table schema. You could then go ahead and parse every line.

$field = "enumField"; // The field that contains the ENUM
$result=mysql_query('show columns from '.$table.';');
while($tuple=mysql_fetch_assoc($result))
{
    if($tuple['Field'] == $field)
    {
        $types=$tuple['Type'];
        $beginStr=strpos($types,"(")+1;
        $endStr=strpos($types,")");
        $types=substr($types,$beginStr,$endStr-$beginStr);
        $types=str_replace("'","",$types);
        $types=split(',',$types);
        if($sorted)
            sort($types);
        break;
    }
} 

You now have an array containing the possible values of your ENUM in $types.
Note: That code is a quick-hack. Could be a little more tidy :)

halfdan
Maybe a 'show columns from $table like $field' to get rid of the while loop ?
edorian
please have a look i have updated the post
sagarmatha
A: 

this works for enums as well as for sets

ssc