Skip to content

Instantly share code, notes, and snippets.

@jeffa
Created August 29, 2013 14:21
Show Gist options
  • Save jeffa/6378737 to your computer and use it in GitHub Desktop.
Save jeffa/6378737 to your computer and use it in GitHub Desktop.
CGI script that turns any database table into a spreadsheet in a web browser.
use strict;
use warnings;
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);
use DBI;
use DBIx::XHTML_Table;
print header(), start_form();
my $DBH = DBI->connect(
qw(DB CONNECT INFO HERE),
{RaiseError => 1, AutoCommit => 1}
);
if (param('go')) {
my %hash;
foreach (param()) {
my ($pk,$field) = $_ =~ /_(\d+)=(\w+)/;
next unless $pk;
$hash{$pk}->{$field} = param($_);
}
while (my($pk,$row) = each %hash) {
$DBH->prepare(
'update <INSERT_TABLE_NAME> set ' .
join(',', map {"$_=?"} keys %$row) .
' where id=?'
)->execute((values %$row),$pk);
}
}
my $table = DBIx::XHTML_Table->new($DBH) or die 'whoops';
$table->set_pk(<INSERT_PK_NAME>);
$table->exec_query('select * from <INSERT_TABLE_NAME>');
$table->map_cell(sub {
my $datum = shift;
my $row = $table->get_current_row();
my $col = $table->get_current_col();
return qq|<input type="text" name="_$row=$col" value="$datum">|;
});
print $table->output(), submit(-name=>"go"),
end_form(), end_html();
$DBH->disconnect();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment