in reply to Re^2: How to get input text boxes populated
in thread How to get input text boxes populated

Hi, I have tried to write the edit script like Poj did but its throwing errors and I have no clue how to fix them : Here is the scrip :
#!/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!<option value="">select name</option>!; for my $row (@$ar) { $options .= qq!<option value="$row->[0]">$row->[1]</option>\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!<h1 style="color:3300CC">Please update an employee</h1>!; # Fetch data if ( $action eq "FETCH" ) { #print qq!<h3>Please make the necessary updates to $empid ?</h3> 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! <form action="" method="post"> Employee ID : $hr->{'EmployeeID'}<br/> <input type="hidden" name="EmployeeID" value="$hr->{'EmployeeID'}"/> Last Name :<input name="lastname" value="$hr->{'LastName'}"/><br/> First Name :<input name="firstname" value="$hr->{'FirstName'}"/><br/> Title :<input name="title" value="$hr->{'Title'}"/><br/> Title Of Courtesy :<input name="toc" value="$hr->{'TitleOfCourtesy'}"/ +><br/> Birth Date :<input name="dob" value="$hr->{'BirthDate'}"/><br/> Hire Date :<input name="doh" value="$hr->{'HireDate'}"/><br/> Address :<input name="address" value="$hr->{'Address'}"/><br/> City :<input name="city" value="$hr->{'City'}"/><br/> Region :<input name="region" value="$hr->{'Region'}"/><br/> PostalCode :<input name="pcode" value="$hr->{'PostalCode'}"/><br/> Country :<input name="country" value="$hr->{'Country'}"/><br/> Home Phone :<input name="homephone" value="$hr->{'HomePhone'}"/><br/> Extension :<input name="ext" value="$hr->{'Extension'}"/><br/> Notes :<input name="notes" value="$hr->{'Notes'}"/><br/> <input type="submit" name="go" value="UPDATE"/> </form>!; my $count = $dbh->do( $sql,undef,$empid ); $msg = "$count Record fetched - $sql, $empid"; } else { print qq!<div class="container"> Select Employee to be updated : <form method="post" action=""> <select name="empid"> $options </select><br/> <input type="submit" name="go" value="FETCH"/> </form></div><hr/>!; # Standard links to the rest of the application print <<"FOOTER"; <b>$msg</b> <hr/> Jump to - <a href="emp2.pl">View Employees Listing</a><br/> Jump to - <a href="addemp.pl">Add an Employee</a><br/> Jump to - <a href="updatephoto.pl">Add or update Employee Photo</a><br +/> <hr/> 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; }
Here are the errors:
Global symbol "%input" requires explicit package name at /usr/share/pe +rlproj/cgi-bin/edit.pl line 19. Global symbol "%input" requires explicit package name at /usr/share/pe +rlproj/cgi-bin/edit.pl line 19. Global symbol "%input" requires explicit package name at /usr/share/pe +rlproj/cgi-bin/edit.pl line 19. Global symbol "%input" requires explicit package name at /usr/share/pe +rlproj/cgi-bin/edit.pl line 19. Global symbol "%input" requires explicit package name at /usr/share/pe +rlproj/cgi-bin/edit.pl line 19. Global symbol "%input" requires explicit package name at /usr/share/pe +rlproj/cgi-bin/edit.pl line 19. Global symbol "%input" requires explicit package name at /usr/share/pe +rlproj/cgi-bin/edit.pl line 19. Global symbol "%input" requires explicit package name at /usr/share/pe +rlproj/cgi-bin/edit.pl line 19. Global symbol "%input" requires explicit package name at /usr/share/pe +rlproj/cgi-bin/edit.pl line 19. Global symbol "%input" requires explicit package name at /usr/share/pe +rlproj/cgi-bin/edit.pl line 19. Global symbol "%input" requires explicit package name at /usr/share/pe +rlproj/cgi-bin/edit.pl line 19. Global symbol "%input" requires explicit package name at /usr/share/pe +rlproj/cgi-bin/edit.pl line 19. Global symbol "%input" requires explicit package name at /usr/share/pe +rlproj/cgi-bin/edit.pl line 19. Global symbol "%input" requires explicit package name at /usr/share/pe +rlproj/cgi-bin/edit.pl line 19. Global symbol "%input" requires explicit package name at /usr/share/pe +rlproj/cgi-bin/edit.pl line 19. Execution of /usr/share/perlproj/cgi-bin/edit.pl aborted due to compil +ation errors.
Its complaining on $input and I don't know what to do, this format of script that I copied from Poj's style is much easier for you monks to review, I hope. Many thank for your help in advance. Rgds Terry

Replies are listed 'Best First'.
Re^4: How to get input text boxes populated
by marto (Cardinal) on Jul 07, 2014 at 11:30 UTC
Re^4: How to get input text boxes populated
by poj (Abbot) on Jul 07, 2014 at 11:51 UTC
    Use placeholders (?) in your sql and extract the data from the parameters.
    if ( ($action eq "UPDATE") && ($empid =~ /\d+/)) { my @data=(); my @fields = qw!lastname firstname title toc dob doh address city region pcode country homephone ext notes repto!; for my $f (@fields)[ push @data,$q->param($f) || '' ; } push @data,$empid; my $sql = qq!UPDATE "Employees" SET "LastName" = ?, "FirstName" = ?, "Title" = ?, "TitleOfCourtesy" = ?, "BirthDate" = ?, "HireDate" = ?, "Address" = ?, "City" = ?, "Region" = ?, "PostalCode" = ?, "Country" = ?, "HomePhone" = ?, "Extension" = ?, "Notes" = ?, "ReportsTo" = ? WHERE "EmployeeID" = ? !; my $count = $dbh->do( $sql,undef,@data); $msg = "$count Record updated - $sql, @data"; } else { $msg = "Please complete form"; }
    poj
Re^4: How to get input text boxes populated
by terrykhatri (Acolyte) on Jul 07, 2014 at 13:30 UTC
    Hi Poj, I changed it to empid, but the behavior is still the same, still its not executing the update query, no updates are being sent to the database and even worse no errors are being displayed. There's got to be something else. Rgds Terry

      "still the same, still its not executing the update query, no updates are being sent to the database and even worse no errors are being displayed. There's got to be something else"

      It seems like a good time for you to start debugging this problem for yourself to find out what that "something else" is. Tutorials->Debugging and Optimization->Basic debugging checklist.

      Update: Also, you're replying to yourself. Consider reading and understanding PerlMonks for the Absolute Beginner.

      To understand why the update block is not being executed add a debug line into the else part.
      } else { $msg = "Please complete form"; $msg.= "<br/>Debug : action=[$action] empid=[$empid]"; }
      You will have to change the regex $empid =~ /\d+/ in the if() to some other pattern if your EmployeeID's don't contain a number, or maybe just use $empid ne ''
      poj
Re^4: How to get input text boxes populated
by terrykhatri (Acolyte) on Jul 07, 2014 at 19:43 UTC
    I just ran Crap::Always against the script and got the following indication:
    perl -MCarp::Always edit2.pl Use of uninitialized value $action in string eq at edit2.pl line 22 (# +1)
    Which is the Action UPDATE line, makes any sense ? Tks Terry
Re^4: How to get input text boxes populated
by terrykhatri (Acolyte) on Jul 07, 2014 at 19:54 UTC
    Norhing happened, it sends me back to start page where you select the employee to be updated, with following lines for the msg :
    Please complete form Debug : action=[UPDATE] empid=[]
    Tks Terry

      If you write more subroutine, so instead of having 100 line if/else blocks, you have a single 10-line if else block, and nine 10-line subroutines, you can figure it out why it sends you back to start page instead of updating

      Its very difficult to reason about a 100-line if/else block but a 10-line if/else block is easy, see Re: Adding a pop up confirmation box

      #!/usr/bin/perl -- use strict; use warnings; Main( @ARGV ); sub Main { my $q = CGI->new({ @_ }); my $action = $q->param('go'); if( !defined $action or !length $action ){ return ShowEmployees( $q ); } elsif( $action eq 'confirmDelete' ){ return ConfirmDeleteEmployees( $q ); } elsif( $action eq 'delete' ){ return DeleteEmployees( $q ); } else { return UnrecognizedAction( $q ); }

      Why doesn't employee get updated?

      Does the UpdateEmployee function get called?

      Does the UpdateEmployee function get all the needed arguments/parameters?

      What arguments does UpdateEmployee get?

      Does UpdateEmployee actually work when it gets good args?

      #!/usr/bin/perl -- use strict; use warnings; use CGI; Main( @ARGV ); sub Main { my $q = CGI->new({ @_ }); my $action = $q->param('go'); if( !defined $action or !length $action ){ return ShowEmployees( $q ); } elsif( $action eq 'update' ){ return UpdateEmployee( $q ); } elsif( $action eq 'delete' ){ return DeleteEmployees( $q ); } else { return UnrecognizedAction( $q ); } } sub UpdateEmployee { my( $q ) = @_; my $name = $q->param('name'); my $id = $q->param('id'); print "name($name) id($id)\n"; } __END__ $ perl ogg.pl Undefined subroutine &main::ShowEmployees called at ogg.pl line 9. $ perl ogg.pl go fish Undefined subroutine &main::UnrecognizedAction called at ogg.pl line 1 +5. $ perl ogg.pl go update Use of uninitialized value $name in concatenation (.) or string at ogg +.pl line 22. Use of uninitialized value $id in concatenation (.) or string at ogg.p +l line 22. name() id() $ perl ogg.pl go update name bob Use of uninitialized value $id in concatenation (.) or string at ogg.p +l line 22. name(bob) id() $ perl ogg.pl go update name bob id 77 name(bob) id(77)

      see also another helper sub DebugCGI, see Basic debugging checklist and brian's Guide to Solving Any Perl Problem and write more subs, so you can debug small subs, not giant programs,

Re^4: How to get input text boxes populated
by terrykhatri (Acolyte) on Jul 08, 2014 at 09:49 UTC
    Hi Poj, First of all thank you very very much for all your help. The problem finally got solved, I had also posted it on PERLGURU and a Guru there his name is Chris noticed a typo in our script, I was calling a wrong hash key at:
    <input type="hidden" name="empid" value="$hr->{'empid'}"/>
    When I changed it to EmployeeID it worked. Thanks once again for your kind help. I have already voted you on this thread and other one. Rgds Terry
Re^4: How to get input text boxes populated
by terrykhatri (Acolyte) on Jul 09, 2014 at 07:31 UTC
    Hi, What should be the Regex if data contains just alphabet letters for the following :
    if ( ($action eq "UPDATE") && ($custid =~ /\d+/)) {
    CustomeIDs are like :
    CustomerID ------------ ALFKI ANATR ANTON AROUT BERGS BLAUS BLONP BOLID
    Many thanks Terry
Re^4: How to get input text boxes populated
by terrykhatri (Acolyte) on Jul 07, 2014 at 12:33 UTC
    Hi Poj, What happens now is when I press UPDATE it sends me back to the first screen to select an employee to be updated, its not updating anything, worse is that its not even sending update query to the database, can you PLEASE go over the script again to see where things are going wrong. Here is the updated script :
    #!/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 @data=(); my @fields = qw!lastname firstname title toc dob doh address city region pcode country homephone ext notes !; for my $f (@fields){ push @data,$q->param($f) || '' ; } push @data,$empid; my $sql = qq!UPDATE "Employees" SET "LastName" = ?, "FirstName" = ?, "Title" = ?, "TitleOfCourtesy" = ?, "BirthDate" = ?, "HireDate" = ?, "Address" = ?, "City" = ?, "Region" = ?, "PostalCode" = ?, "Country" = ?, "HomePhone" = ?, "Extension" = ?, "Notes" = ?, WHERE "EmployeeID" = ? !; my $count = $dbh->do( $sql,undef,@data); $msg = "$count Record updated - $sql, @data"; } 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!<option value="">select name</option>!; for my $row (@$ar) { $options .= qq!<option value="$row->[0]">$row->[1]</option>\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!<h1 style="color:3300CC">Please update an employee</h1>!; # Fetch data if ( $action eq "FETCH" ) { #print qq!<h3>Please make the necessary updates to $empid ?</h3> 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! <form action="" method="post"> Employee ID : $hr->{'EmployeeID'}<br/> <input type="hidden" name="EmployeeID" value="$hr->{'EmployeeID'}"/> Last Name :<input name="lastname" value="$hr->{'LastName'}"/><br/> First Name :<input name="firstname" value="$hr->{'FirstName'}"/><br/> Title :<input name="title" value="$hr->{'Title'}"/><br/> Title Of Courtesy :<input name="toc" value="$hr->{'TitleOfCourtesy'}"/ +><br/> Birth Date :<input name="dob" value="$hr->{'BirthDate'}"/><br/> Hire Date :<input name="doh" value="$hr->{'HireDate'}"/><br/> Address :<input name="address" value="$hr->{'Address'}"/><br/> City :<input name="city" value="$hr->{'City'}"/><br/> Region :<input name="region" value="$hr->{'Region'}"/><br/> PostalCode :<input name="pcode" value="$hr->{'PostalCode'}"/><br/> Country :<input name="country" value="$hr->{'Country'}"/><br/> Home Phone :<input name="homephone" value="$hr->{'HomePhone'}"/><br/> Extension :<input name="ext" value="$hr->{'Extension'}"/><br/> Notes :<input name="notes" value="$hr->{'Notes'}"/><br/> <input type="submit" name="go" value="UPDATE"/> </form>!; my $count = $dbh->do( $sql,undef,$empid ); $msg = "$count Record fetched - $sql, $empid"; } else { print qq!<div class="container"> Select Employee to be updated : <form method="post" action=""> <select name="empid"> $options </select><br/> <input type="submit" name="go" value="FETCH"/> </form></div><hr/>!; # Standard links to the rest of the application print <<"FOOTER"; <b>$msg</b> <hr/> Jump to - <a href="emp2.pl">View Employees Listing</a><br/> Jump to - <a href="addemp.pl">Add an Employee</a><br/> Jump to - <a href="updatephoto.pl">Add or update Employee Photo</a><br +/> <hr/> 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; }
    Many many thanks in advance. Rgds Terry
      In the update form, change the name in the hidden input field to empid.
      <input type="hidden" name="empid" value="$hr->{'EmployeeID'}"/>
      otherwise the validation here will prevent the update
      if ( ($action eq "UPDATE") && ($empid =~ /\d+/)) {
      poj