1. No Profile Picture
    Devshed Newbie (0 - 499 posts)
    I have a fairly complicated form set up that uses a lot of "check all that apply" type questions (input type="checkbox" var name="blah[]" value="blah_value"). Now, I know the brackets mean that it should be passing whichever values have been checked off to my script and then to the database. My problem is, most of the values aren't making it into the database. Something's getting passed along, just not all the data. Where am I going wrong?

    I've tried 5 different INSERT syntaxes:

    INSERT INTO info VALUES('$prog_descr_text', '$purpose[]') gets me an SQL error which says it's expecting a number or $ in brackets.

    INSERT INTO info VALUES('$prog_descr_text', '$purpose[0]') passes along the first checked value and nothing else.

    INSERT INTO info VALUES('$prog_descr_text', '$purpose[$purpose]') passes along the last checked value and nothing else.

    INSERT INTO info VALUES('$prog_descr_text', '$purpose') passes along "Array", but not the actual data.

    INSERT INTO info VALUES('$prog_descr_text', '$purpose[11]') passes along nothing. (11 is the total number of values that could be passed along if all the boxes were checked.)

    Any help is greatly appreciated!

    Thanks in advance.
  2. #2
  3. No Profile Picture
    rod k
    Devshed Newbie (0 - 499 posts)

    You should have left the thread in PHP as your problem is not with MySQL. Your difficulties lie in how you are manipulating the PHP array. $purpose is an array. You cannot place the contents of an array into a single mysql field. The simplest way of doing this is to implode() the array into a single variable and store that in the table. Then, when you pull the field, you'd have to explode() the variable into an array.

    Another option is to have a binary field for each option with a 0 default, then change it to 1 if the option was selected.

    Both methods have positives and negatives. The best for you depends on what you plan on doing with the data.

    To help you understand I'll comment on the 5 inserts you've described.

    The first is invalid as you can only use a non-indexed array variable in an assignment. Saying:

    print $purpose[];

    has no meaning, whereas:


    is valid because PHP will place "information" into the array in the next unassigned index. (e.g. if $purpose[0] and $purpose[1] are assigned but $purpose[2] is not than $purpose[2]=="information";

    The second insert gives the first value because php has assigned the first checkbox name (that was checked) to the 0 index.

    I'm not sure why the third insert works the way it does. If you refer to an array without brackets (as you did in example 4) it should return "Array" as you found out. So what you are saying is $purpose[Array] which should be null. Probably a quirk in php.

    Insert 5 doesn't work because $purpose[11] is null even if all boxes are checked. If 11 is the total number your checkbox names are stored in $purpose[0] thru $purpose[10].


  4. #3
  5. No Profile Picture
    Devshed Newbie (0 - 499 posts)
    The answers were helpful in letting me know where I was screwing things up. Since I'm under some time pressure for this project, I think the quickest solution is to make each checkbox its own table field, and save the arrays for when I'm a little more comfortable with both PHP and MySQL syntax for the second version of this database.

    Thanks so much for your help!

  6. #4
  7. No Profile Picture
    Devshed Newbie (0 - 499 posts)
    You put all your checkbox-outputs into a blah[] array. First thing to know is that when you check a box, it puts the value of that checkbox into blah[] (first checked value in blah[0], second checked value in blah[1] etc.).
    So in PHP it is now easy to get the size of the blah[] array by: $blahCount = count($blah).
    Then do the insert as:
    for ($i=0; $i < $blahCount; $i++) {
    $insertquery = INSERT INTO info VALUES('$prog_descr_text', '$blah[$i]');
    This should do the job, quick and clean.

    Good luck, Peter

Similar Threads

  1. Errcode:13 . How to solve it on MySQL + Windows 2000
    By hankycheng in forum MySQL Help
    Replies: 28
    Last Post: August 3rd, 2003, 10:05 AM
  2. Replies: 3
    Last Post: January 7th, 2003, 11:57 PM
  3. inserting form data to mysql problem
    By jnk in forum PHP Development
    Replies: 2
    Last Post: October 6th, 2002, 10:06 PM
  4. Replies: 2
    Last Post: July 8th, 2002, 07:31 AM
  5. 'flashing' through divs
    By Roxy in forum HTML Programming
    Replies: 4
    Last Post: April 16th, 2002, 06:15 PM

IMN logo majestic logo threadwatch logo seochat tools logo