my $stmt = qq/INSERT INTO books ( / . join(',', keys %hash) . qq/) VALUES (/ . join(',', ('?') x keys %hash) . qq/)/; my $sth = $dbh->prepare($stmt); $sth->execute(values %hash); #### sub write_to_db { my $table = shift; #pass in table name my $hash = shift; #pass in hash ref to data my ($stmt, $sth); #subroutine to connect to db my $dbh = dbconnect(server =>'master', db => 'member'); my %hash = %{ $hash }; #deref data if ($query->param('update')) { #set a hidden value in html form if updating $stmt = qq/UPDATE $table SET updated_on = NOW(), / . join(' = ?,', keys %hash) . qq/ = ? WHERE id = ?/; $sth = $dbh->prepare($stmt); $sth->execute(values %hash, $query->param('id')); #set a hidden value } else { my $stmt = qq/INSERT INTO $table (created_on, / . join(',', keys %hash) . qq/) VALUES (NOW(),/ . join(',', ('?') x keys %hash) . qq/)/; my $sth = $dbh->prepare($stmt); $sth->execute(values %hash); } }