if ($submit):

$sql = "INSERT INTO my_table SET
id='$id',
category='$category',
subcategory='$subcategory'";

if (@mysql_query($sql1)) {
	echo("<P><font size=-1 face=arial><b>Category Set entered into 
Gallery database, now go fill in the important information.</P>");
} else {

echo("<P><font size=-1 face=arial><b>somethings messed up 
with db.</P>" . mysql_error() . "</p>");
	}

I'm making a form to insert new categories. The categories field is a ENUM. The ID and SUBCATEGORY fields are int and text fields.

What I want to do is be able to add new categories to my Category ENUM and Insert new data into the ID, Subcategory fields.

Does anyone know how to populate a ENUM set through a form using PHP / HTML? I can't seem to get the insert command right.

Any help would be greatly appreciated!

Thanks in advance
Rob

    You will need an ALTER TABLE command to change the list of possible values for an enum field, since the values are part of the definition of the table, not the data.

    If you are going to update the list of allowable values regularly, then using an ENUM type probably isn't the most suitable option.

    hth, njm

      To update the field use the following:

      For the ENUM - you can have up to 65535 different types - to update the row for this just use :

      UPDATE bose SET visible = 'no' WHERE ref = '1' LIMIT 1

      VISIBLE is the enum type.

      You just need to refer to it by the specific type.

      See http://www.mysql.com/doc/en/ENUM.html for more details

      To add more options to the enum - you will have to use the ALTER TABLE option as previously stated.

        Would it go something like this?

        if ($submit): 
        
        $sql1 = "ALTER TABLE `my_table` CHANGE `category` `category` ENUM(\'$category\')"; 
        
        $sql2 = "INSERT INTO my_table SET 
        id='$id', 
        subcategory='$subcategory'"; 
        
        if (@mysql_query($sql1)) { 
            echo("<P><font size=-1 face=arial><b>Category Set entered into 
        Gallery database, now go fill in the important information.</P>"); 
        } else { 
        
        echo("<P><font size=-1 face=arial><b>somethings messed up 
        with db.</P>" . mysql_error() . "</p>"); 
        
        if (@mysql_query($sql2)) { 
            echo("<P><font size=-1 face=arial><b>Category Set entered into 
        Gallery database, now go fill in the important information.</P>"); 
        } else { 
        
        echo("<P><font size=-1 face=arial><b>somethings messed up 
        with db.</P>" . mysql_error() . "</p>"); 
            }
        
        
        

          Do you try it?

          $sql1 = "ALTER TABLE `my_table` CHANGE `category` `category` ENUM('$category')";

          i think will change the category field, so $category is the only allowable value. You would need to add all the ENUM types into the SQL statement. Also:

          $sql2 = "INSERT INTO my_table SET 
              id='$id',   subcategory='$subcategory'";

          doesn't actually set the value of the category field for that record. As I said above, if you're going to be changing the allowable values for a field regularly, then you probably don't want an ENUM field because of the pain of writing ALTER TABLE statements each time

          hth, njm

            well i came up with a solution. I changed the field type from a ENUM to a Text Field and i changed the SQL statement.

            I guess what I wanted it to do when i was using an ENUM was just add new selections to the database w/o having to list all the already existing ENUM in the select statement.

            That would defeat the purpose. Everytime i added a ENUM selection i'd have to update that page.

            I just wanted to use a form, enter what new selections i want added and click submit and have it add a new ENUM selection to the ENUM set.

            I'd still like to know how to do that, but i have since moved on so its not critical anymore.

              Write a Reply...