foreach $table ( @table_names ) {
foreach $key (keys %param) {
unless ($key =~ m/^$table/) {next}
print STDOUT ("
***Debug, key=$key
\n")
if $debug eq 'yes';
if ($key =~ m/skill/) {
$skill = $param{$key};
$value = $key;
$value =~ s/.*_skill_(.*)/$1/;
$table_value = $table . '_' . $value;
print STDOUT ("
***debug, skill=$skill\n",
" and value=$value
\n")
if $debug eq 'yes';
print STDOUT (
"
***debug, table_value=$table_value\n",
" and value=$value
\n")
if $debug eq 'yes';
if (exists $param {$table_value} and
defined $param {$table_value} ) {
$real_value = $param {$table_value};
$ref = $dbh->selectcol_arrayref
("SHOW COLUMNS FROM $table");
print STDOUT (
"
***debug after call to show columns\n", "Error string=$DBI::errstr, ", "and error=$DBI::err
\n") if $debug eq 'yes'; @field_names = @{$ref} if defined $ref; print STDOUT ( "***Debug, table=$table, field_names=",
@field_names,
"
\n")
if $debug eq 'yes';
unless (defined $ref) {
&send_message($error,
"Could not get field names for table $table.",
"Error string=$DBI::errstr, and error=$DBI::err");
goto END_OF_PROGRAM;
}
$names_as_string = '';
$place_holders = '';
foreach $field_name (@field_names) {
$names_as_string = $names_as_string .
$field_name . ',';
$place_holders = $place_holders . '?,';
}
$names_as_string =~ s/,$//;
$place_holders =~ s/,$//;
# Slog through all the variables to set up insert.
$i = 0;
foreach $field_name (@field_names) {
if ( $field_name eq 'local_id' ) {
$all_values[$i] = undef;
}
elsif ($field_name eq 'contact_id' ) {
$all_values[$i] = $contact_id;
}
elsif ($field_name =~ m/name$/ ) {
$all_values[$i] = $real_value;
}
elsif ($field_name =~ m/skill/ ) {
$all_values[$i] = $skill;
}
else {
$all_values[$i] = undef;
}
$i++;
}
# Deal with people submitting comments and other text for possible
# additional options to the table. Send them as e-mail to the
# administrator.
if ($other =~ m/\w+/ ) {
print MAIL (
"\ttable is $table\n",
"Comment: $other\n");
$mail_to_send = 'yes';
}
print STDOUT ("
***Debug, table=$table
\n",
"names_as_string=$names_as_string
\n",
"place_holders=$place_holders
\n")
if $debug eq 'yes';
$count = 0;
$sth = $dbh ->prepare
("INSERT INTO $table
($names_as_string) VALUES
($place_holders)");
$count += $sth -> execute((@all_values));
print STDOUT
("***Debug, Error string=$DBI::errstr, ",
"and error=$DBI::err\n")
if $debug eq 'yes';
if ($count > 0) {
print STDOUT (
"
Successfully inserted ",
"$real_value data ",
"into the $table table.\n");
}
}
else {
print STDOUT ("
***debug, param of ",
"table_value=$table_value\n",
" does not exist.\n
")
if $debug eq 'yes';
next;
}
}
else {
next;
}
}
else {
&send_message($error,
"Could not insert data into $table table of the $dev_switch database",
"Error string=$DBI::errstr, and error=$DBI::err");
print STDOUT ("\n
Processing has stopped. The database ", "manager has been notified. Sorry for the ", "inconvenience.
\n"); goto END_OF_PROGRAM; } }