in reply to HTML Form Submission & Save to MySQL
note that this one also contains a html template loop for extracting the tuples out of DB and displaying them, along with a form#!/usr/bin/perl use strict; use CGI; use HTML::Template; use DBI; my $q = CGI->new; my $dbh = DBI->connect( 'dbi:Pg(RaiseError=>1,Taint=>1):dbname=write;host=loca +lhost', 'user', 'password', ); if ( $q->param('post') eq 'post' ) { #post info to db my $sth_get_text = $dbh->prepare( q{ insert into write (name,text) values (?,?) } ); my $insert = $sth_get_text->execute( $q->param('name'), $q->param( +'text') ); $sth_get_text->finish; $q->redirect('/perl/write.pl'); } else { #display my $sth_get_text = $dbh->prepare( q{ select text,name from write } ); $sth_get_text->execute(); my $text = $sth_get_text->fetchall_arrayref; $sth_get_text->finish; my %hash; my $stuff; foreach my $text_in ( @{$text} ) { push @{ $hash{stuff} }, { text => $text_in->[0], name => $text_in->[1], }; } my $template = HTML::Template->new( filename => 'write.tmpl', die_on_bad_params => 0 ); $template->param(%hash); print $template->output; } $dbh->disconnect;
|
|---|