in reply to Putting a long form into a database

So what does the table look like? Does it have all the values as columns in a single row? (And I hope thae columns are named the same as the variables in the web page). Or does it have a "name of the variable" column and a "value" column?

If the former, something like

my $col="insert into table mytable ("; my $val="values ("; foreach (param()) { $col.="$_,"; $val.="?,"; push(@arr,param($_)); # Beware! this will break if any variables hav +e multiple values. } $val=~s/,$//; $col=~s/,$//; $statement="$col) $val)"; $sth=$dbh->prepare($statement); $sth->execute(@arr);
If the table is in name/value pairs, then the code is much simpler:
my $sth=$dbh->prepare("insert into table mytable (name,value) values ( +?,?)"); foreach (param()) { $sth->execute($_,param($_)); # will only insert the first value if a + variable has multiple values }

Replies are listed 'Best First'.
Re: Re: andling form data
by bar10der (Beadle) on Apr 14, 2004 at 14:43 UTC
    Thanks Matija for your help. My table has column names same as field names in the form. So I think 1st part of the code will work for me. However I am not sure about one thing -

    what happens if a field in the form is left blank? Will $val contain null for that field?

      If the browser doesn't pass the empty field (which most browsers don't I think), then it won't be set at all (that is part of the purpose of enumerating the fieldnames in the INSERT statement) and it's value will be whatever default you set up for the table - most probably NULL or empty string.

      If the browser does pass fields without values entered, then they will be set to empty string in the database.