|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Generate data entry and reporting .NET Web apps in minutes, straight from your database. Read our FREE whitepaper “Build Web 2.0 Applications Without Hand-Coding” Download now! |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
Hannah,
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: $purpose[]="information"; 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]. HTH Rod |
|
#3
|
|||
|
|||
|
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! |
|
#4
|
|||
|
|||
|
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]'); mysql_query($insertquery); } This should do the job, quick and clean. Good luck, Peter |
![]() |
| Viewing: Dev Shed Forums > Databases > MySQL Help > Getting input type=checkbox data from a form into MySQL |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|