MySQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesMySQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 18th, 1999, 11:19 AM
hannnah
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
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.

Reply With Quote
  #2  
Old November 18th, 1999, 12:21 PM
rod k
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
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

Reply With Quote
  #3  
Old November 18th, 1999, 12:43 PM
hannnah
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
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!


Reply With Quote
  #4  
Old December 14th, 1999, 10:06 AM
PAV
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMySQL Help > Getting input type=checkbox data from a form into MySQL

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap