Forum Moderators: open

Message Too Old, No Replies

Inserting from arrays to Mysql with Perl DBI

Inserting from arrays to Mysql with Perl DBI

         

typomaniac

11:25 pm on Sep 10, 2015 (gmt 0)

10+ Year Member Top Contributors Of The Month



I have a database where, upon submitting a form, some tables are supposed to be able to recieve multiple rows, depending on the user input. For example:

my $med1="Award Number 1";
my $med2="Award Number 2";
my $med3="Award Number 3";

my %medals=(
"$med1"=>"$me1",
"$med2"=>"$me2",
"$med3"=>"$me3",

my $table="table";
my $state="INSERT INTO ".$table." (uid,mid,qty) VALUES (LAST_INSERT_ID(),?,?)";
foreach (keys%medals){
if($medals{$_}>0){
my $sth=$dbh->prepare($state);
$sth->execute ($_ ,$medals{$_});
}
}


This comes from a bunch of text inputs put into a hash so as to get the name of an award and how many times the award was given. This worked all well and fine but when I tried to use the same code with a different table name and column names it wouldn't work. In the 2nd case (which in the actual script comes before the above code) the original format was to use checkboxes and a simple array, which would only input one line of the array which met the conditions. Same as the above code with dfferent paramaters, it only inputs one row instead of all that met conditions. I have searched high and low for information on using perl/cgi to input information put into an array and have basically come up with nothing that works. Is there something I'm missing here(or, I should say what is it I'm missig) when it comes to doing this. I'm basically down to sets of checkboxes and need to enter them (as many as are checked) into mysql table rows.