Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check boxes and Array

Status
Not open for further replies.

cgiles82

MIS
Mar 31, 2005
3
US
I need to take the values passed from checkboxes in a html form and put all the ones checked into a single field. My array doesn't seem to be working.

my $stuff;

foreach $field (param("County")) {
if ($field->checked) $stuff += "|" + $field->value;
}



my $sql = "INSERT into main (Name,Address,City,State,Phone,County) values
('".param("Name")."',
'".param("Address")."',
'".param("City")."',
'".param("State")."',
'".param("Phone")."',
'".$stuff."'
)";
 
Assuming you're using CGI.pm for your form parsing, the param function will return a list of those checkboxes that were checked, so there's no need for the "if $field->checked" statement.

As an aside, that's not a good way to perform an SQL insert. You're opening yourself up to SQL injection attacks. I'd very strongly advise reading about placeholders, bind parameters and prepared statements.

Finally, the fact that you're trying to join multiple items together to put into a single database field should tell you that your database isn't properly normalised. Just something to think about.
 
I'm not using CGI.pm. Thanks for the suggestions, this is just a quick and dirty internal form.

I will remove the part you suggested.
 
what is this supposed to be doing?

if ($field->checked) $stuff += "|" + $field->value;

+= and + are math operators, not string constructors.

If you're not using CGI.pm, where is the param() function coming from?

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
In my experience, "quick and dirty internal forms" quickly get pushed into production. Usually without checking with the original developer. My take has always been to build it right the first time.

Just my two cents.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top