Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Does anybody have a way of taking this:

<input type="checkbox" name="Name" value="1"> <input type="checkbox" name="Name" value="2"> <input type="checkbox" name="Name" value="3">
And getting it into a MySQL SET column type in the form of:

1,2,3

?

Thanks.

Replies are listed 'Best First'.
Re: mysql, checkbox, set, processing
by InfiniteSilence (Curate) on Aug 31, 2005 at 20:37 UTC
    Is your question how to create a set column in MySQL or are you actually asking how to put the data into MySQL?

    BTW: Here's a piece on why you might consider using something other than a SET in MySQL.

    Celebrate Intellectual Diversity

      Yes, set seems like a good idea at first glance but it WILL bite you later on. When you set up the table definition, you specify all the values that are acceptable in the set. Then when you realize you need another valid value in the set, you need to alter the table definition. And you need to keep the original set items in the same order because the storage is actually numeric aliases, and when you redefine the column, it doesnt woory about the stored values. When you want to remove a value from the set the situation is worse, because then you can't retain the same positioning of all the set elements in the definition.

      A much better solution is to use a lookup table with the valid values you'd put in the set. But you need an intermediate table to facilitate the many-to-many relation that occurs with a set.

      Similar arguements for the ENUM datatype, but easier to implement with the lookup table because there is no many-to-many relation.


      I use the most powerful debugger available: print!
Re: mysql, checkbox, set, processing
by mwp (Hermit) on Aug 31, 2005 at 21:15 UTC
    Well, quite simply,
    # prepare query to insert set my $sth = $dbh->prepare('INSERT INTO myset (col) VALUES (?)'); # build set to insert my $set = join ',', $cgi->param('Name'); # insert set $sth->execute($set);
    But InfiniteSilence is asking all the right questions...

    Updated to include ikegami's corrections.

    Alakaboo

      Shouldn't that be $cgi->param('Name'), and
      are you sure those parens are suppose to be in $set?

        Yes, no, and thank you!

        alakaboo

Re: mysql, checkbox, set, processing
by bradcathey (Prior) on Sep 01, 2005 at 01:24 UTC

    I have to be honest, I don't understand what you are doing. Why would you be using 3 check boxes with the same name, but different values? How would you repopulate the form if you had to? "Name=1?" Which Name? I think you mean radio buttons, but then you would only be storing the selected value.

    Are you sure you don't mean to have 3 different names for those check boxes? In which case you need to normalize the table and use 3 columns, one column for each (not a good practice to place more than one value in a column, especially if you have to parse it when you retrieve it later).

    Sorry if I'm missing anything.


    —Brad
    "The important work of moving the world forward does not wait to be done by perfect men." George Eliot
      Alakaboo -- thanks. I'm new to perl and didn't realize it would successfully post multiple values of the same name.

      Brad -- I'm building a form to handle research data.

      So, are you:
      A. Sick to your stomach
      B. Sick in your head
      C. Sick in your feet

      So the question is: are you sick? How?

      The name is "sick" and the values are A,B,C. That's why a radio wouldn't work.

      Shemp, Creating a lookup table is definitely the plan as we develop our platform for creating this type of survey in the future, but for this "one-off" form, it would be overkill. You make some great point, though, so I guess a seperate column for each checkbox is the way to go... for the reasons you mentioned, both indexing and adding/removing options.

      Thanks everybody, I'm new to Perl, but I knew this would be the place to ask.

      John Herr

        It's not really a Perl question, it's more of an organizational one. Bottomline, if you can be sick with all three, then use checkboxes. If only one, then use radio.

        In your database you would have a table for all the kinds of sickness, a table for the patient id, and a table with a column for the patient id and one for the type of sickness id. You would join the patient and tables to do a query on any one patient.

        HTML: <input type="checkbox" name="stomach" value="1" /> <input type="checkbox" name="head" value="2" /> <input type="checkbox" name="feet" value="3" /> SICKNESS TABLE: +----+---------+ | id | name | +----+---------+ | 1 | stomach | +----+---------+ | 2 | head | +----+---------+ | 3 | feet | +----+---------+ PATIENT TABLE: +----+---------+ | id | name | +----+---------+ | 1 | Bill | +----+---------+ | 2 | Susan | +----+---------+ | 3 | Mary | +----+---------+ STATUS TABLE: +------------+----------+ | patient id | sickness | +------------+----------+ | 1 | 2 | +------------+----------+ | 1 | 3 | +------------+----------+ | 2 | 1 | +------------+----------+

        Insert a row into the "patient-sickness" table for each type of sickness the patient has checked.


        —Brad
        "The important work of moving the world forward does not wait to be done by perfect men." George Eliot