#!/usr/local/bin/perl use strict; use CGI; use CGI::Carp 'fatalsToBrowser'; #remove for prod use DBI; # get form parameters my $q = new CGI; my $action = $q->param('go'); my $empid = $q->param('empid'); my $dbh = dbh(); # connect to db $dbh->do("SET search_path to northwind") or die; # If the confirm form was properly submitted, update the record my $msg; # change validation to suit if ( ($action eq "UPDATE") && ($empid =~ /\d+/)) { my $sql = qq!UPDATE "Employees" SET "LastName" = "." \'$input{lastname}\', "FirstName" = "." \'$input{firstname}\', "Title" = "." \'$input{title}\', "TitleOfCourtesy" = "." \'$input{toc}\', "BirthDate" = "." \'$input{dob}\', "HireDate" = "." \'$input{doh}\', "Address" = "." \'$input{address}\', "City" = "." \'$input{city}\', "Region" = "." \'$input{region}\', "PostalCode" = "." \'$input{pcode}\', "Country" = "." \'$input{country}\', "HomePhone" = "." \'$input{homephone}\', "Extension" = "." \'$input{ext}\', "Notes" = "." \'$input{notes}\', "ReportsTo" = "." \'$input{repto}\' WHERE "EmployeeID" = ? !; my $count = $dbh->do( $sql,undef,$empid ); $msg = "$count Record updated - $sql, $empid"; } else { $msg = "Please complete form"; } # get employees my $sql = qq!SELECT "EmployeeID" AS empid, "FirstName"::text || ' ' ||"LastName"::text AS name FROM "Employees" !; my $ar = $dbh->selectall_arrayref($sql); # Make up a pulldown menu my $options = qq!!; for my $row (@$ar) { $options .= qq!\n!; } # build html page my $style = q! body { background-color: pink ; color: #3300cc; } .container { width: 500px; clear: both; } .container input { width: 100%; clear: both;} !; # Send out the header and form print $q->header; print $q->start_html(-title=>'Update an employee record', -style=>{ -code=>$style } ); print qq!

Please update an employee

!; # Fetch data if ( $action eq "FETCH" ) { #print qq!

Please make the necessary updates to $empid ?

my $sql = 'SELECT * FROM "Employees" WHERE "EmployeeID" = ?'; my $hr = $dbh->selectrow_hashref($sql,undef,$empid); #my $hr = $dbh->selectrow_hashref($sql,undef,$input{empid}); print qq!
Employee ID : $hr->{'EmployeeID'}
Last Name :
First Name :
Title :
Title Of Courtesy :
Birth Date :
Hire Date :
Address :
City :
Region :
PostalCode :
Country :
Home Phone :
Extension :
Notes :
!; my $count = $dbh->do( $sql,undef,$empid ); $msg = "$count Record fetched - $sql, $empid"; } else { print qq!
Select Employee to be updated :


!; # Standard links to the rest of the application print <<"FOOTER"; $msg
Jump to - View Employees Listing
Jump to - Add an Employee
Jump to - Add or update Employee Photo

Edited by Terry on July, 06 2014. FOOTER } print $q->end_html; # connect to database sub dbh { my $dsn = 'DBI:Pg:dbname=northwind;host=localhost'; my $user = 'postgres'; my $pwd = 'postgres'; my $dbh = DBI -> connect($dsn,$user,$pwd,{'RaiseError' => 1}); return $dbh; }