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