in reply to How to retain a value passed from another link to a script
You need to show your code if you want help with it.
First thoughts:
- Show the function that can't see the value
- Show the function that can see the value
- Show the differences between the two functions; where they are located, how they are called.
Re^2: How to retain a value passed from another link to a script
by terrykhatri (Acolyte) on Jul 11, 2014 at 18:18 UTC
|
Hi,
Here is the script, the ELSE routine of Action EDIT gets execute, when I click on EDIT button this Action does not get excuted the form shows up empty with $ordered as null,
I can't get to Action UPDATE because it comes after Action EDIT.
#!/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 $orderid = $q->param('orderid');
my $msg = "";
my $dbh = dbh(); # connect to db
$dbh->do("SET search_path to northwind") or die;
my $sql = 'SELECT
a."OrderID", b."ProductName", a."UnitPrice", a."Quantity"
+, a."Discount"
FROM
"Order_Details" a, "Products" b
WHERE
a."ProductID" = b."ProductID" AND "OrderID" = ? ';
my $hr = $dbh->selectrow_hashref($sql,undef,$orderid);
#change validation to suit
if ( ($action eq "UPDATE") && ($orderid =~ /\d+/)) {
my $sql = qq! UPDATE
"Order_Details"
SET
"UnitPrice" = ?,
"Quantity" = ?,
"Discount" = ?
WHERE "OrderID" = ? !;
my $count = $dbh->do( $sql,undef,$orderid );
$msg = "$count Record updated - $sql, $orderid";
} else {
$msg = "Please complete form";
}
# 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=>'Order details',
-style=>{ -code=>$style } );
if ( ($action eq "EDIT") && ($orderid =~ /\d+/)) {
print qq!<h1 style="color:3300CC">Update Order Details for order # ?
+</h1>!;
print qq!<div class="container">
<form action="" method="post">
<input type="hidden" name="ordid" value="$hr->{'OrderID'}"/>
Product Name :<input name="productname" value="$hr->{'ProductName'}" r
+eadonly/><br/>
Unit Price :<input name="unitprice" value="$hr->{'UnitPrice'}" /><br/>
Quantity :<input name="quantity" value="$hr->{'Quantity'}" /><br/>
Discount :<input name="discount" value="$hr->{'Discount'}" /><br/>
<input type="submit" name="go" value="UPDATE"/>
</form></div><hr/>!;
$msg = "";
} else {
print qq!<h1 style="color:3300CC">Order Details for order # $orderid</
+h1>!;
print qq!<div class="container">
<form action="" method="post">
<input type="hidden" name="ordid" value="$hr->{'OrderID'}"/>
Product Name :<input name="productname" value="$hr->{'ProductName'}" r
+eadonly/><br/>
Unit Price :<input name="unitprice" value="$hr->{'UnitPrice'}" readonl
+y/><br/>
Quantity :<input name="quantity" value="$hr->{'Quantity'}" readonly/><
+br/>
Discount :<input name="discount" value="$hr->{'Discount'}" readonly/><
+br/>
<input type="submit" name="go" value="EDIT"/>
</form></div><hr/>!;
$msg = "Record details fetched - $sql, $orderid";
}
# Standard links to the rest of the application
print <<"FOOTER";
<b>$msg</b>
<hr/>
Jump to - <a href="viewemp.pl">View Employees Listing</a><br/>
Jump to - <a href="addemp.pl">Add an Employee</a><br/>
Jump to - <a href="editemp.pl">Edit an Employee details</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;
}
| [reply] [d/l] |
|
Spot the parameter name difference
my $orderid = $q->param('orderid');
<input type="hidden" name="ordid" value="$hr->{'OrderID'}"/>
When you fix that this will fail (not enough bind variables)
my $sql = qq! UPDATE
"Order_Details"
SET
"UnitPrice" = ?,
"Quantity" = ?,
"Discount" = ?
WHERE "OrderID" = ? !;
my $count = $dbh->do( $sql,undef,$orderid );
poj | [reply] [d/l] [select] |
|
Many thanks Poj, You are the only ONE :)
OK you are right the update is failing I changed the code though :
if ( ($action eq "UPDATE") && ($orderid =~ /\d+/)) {
my @data=();
my @fields = qw!unitprice quantity discount !;
for my $f (@fields){
push @data,$q->param($f) || '' ;
}
push @data,$orderid;
my $sql = qq! UPDATE
"Order_Details"
SET
"UnitPrice" = ?,
"Quantity" = ?,
"Discount" = ?
WHERE "OrderID" = ? !;
my $count = $dbh->do( $sql,undef,$orderid );
$msg = "$count Record updated - $sql, $orderid";
Please help !!! | [reply] [d/l] |
|
|
Many many many thank Poj, you are amazing !!! I have one more queston to ask and that is pagination, I need to paginate a form which has too many records, I have been searching google but I DONT quite understand how to implement it, if you can help me that will be great !!!
Rgds.
Terry
| [reply] |
|
|
Hi Marto,
I have tried to implement it by googling through various examples but no example is working or fitting to my situation, I am learning by getting the code, the help provided to me by your monks specially Poj has helped me do so many other webpages which you don't know, once I am done with this project I am going study all the links given to me in this forum I promise OK !!
Thanks for putting up with me !!
| [reply] |
|
|
Here is the script that needs pagination :
#!/usr/local/bin/perl
use DBI;
use CGI;
use strict;
use HTML::Template;
my $CGI = CGI->new();
my $dbh = dbh(); # connect to db
$dbh->do("SET search_path to northwind") or die;
# grab the stuff from the database
my $sql = qq!SELECT
a."OrderID", b."CompanyName" AS "CustomerName",
c."FirstName"::text || ' ' ||c."LastName"::text AS "E
+mployeeName",
a."OrderDate", a."RequiredDate", a."ShippedDate", d."
+CompanyName" AS "ShipVia",
a."Freight", a."ShipName", a."ShipAddress", a."ShipCi
+ty", a."ShipRegion",
a."ShipPostalCode", a."ShipCountry"
FROM
"Orders" a, "Customers" b, "Employees" c, "Shippers"
+d
WHERE
a."CustomerID" = b."CustomerID" AND
a."EmployeeID" = c."EmployeeID" AND
a."ShipVia" = d."ShipperID"
ORDER BY 1 !;
my $sth = $dbh->prepare($sql);
$sth->execute();
# prepare a data structure for HTML::Template
my $rows;
push @{$rows}, $_ while $_ = $sth->fetchrow_hashref();
# instantiate the template and substitute the values
my $template = HTML::Template->new(filename => 'Orders.tmpl');
$template->param(ROWS => $rows);
print $CGI->header();
print $template->output();
$dbh->disconnect();
# 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;
}
If you need the template code used in this script I will post it.
Many thank !!! | [reply] [d/l] |
|
|
|