08-19-2006
|
#1 (permalink)
|
|
Member
Join Date: Jul 2006
Age: 25
Posts: 53
|
Database Programming with Perl - an introduction to MySQL and DBI
Order Program Using DBI
Quote:
|
#!/usr/bin/perl -wTuse CGI qw(:standard);use CGI::Carp qw(warningsToBrowser fatalsToBrowser);use DBI;use strict;print header;print start_html("Order Form - Step 2");my $dbh = DBI->connect( "dbi:mysqlroducts", "webserver", "", { RaiseError => 1, AutoCommit => 1 }) or &dienice("Can't connect to database: $DBI::errstr");print <<EndHead;<h2 align="CENTER">Order Form - Step 2</h2>Here's what you've ordered:<br><form action="order2.cgi" method="POST">EndHeadmy $sth = $dbh->prepare(qq(select stocknum,name,price from items where status != "OUT" order by stocknum)) or &dbdie;$sth->execute or &dbdie;my $subtotal = 0;while (my($stocknum,$name,$price) = $sth->fetchrow_array) { if (param($stocknum)) { my($qty) = param($stocknum); $subtotal = $subtotal + ($price * $qty); print qq(<b>$name</b> (#$stocknum) - $price ea., qty: $qty<br>\n); print qq(<input type="hidden" name="$stocknum" value="$qty">\n); }}if ($subtotal == 0 ) { &dienice("You didn't order anything!");}print <<EndForm;<p>Subtotal:<br> \$$subtotal<p>Please enter your shipping information:<br><br><pre> Your Name: <input type="text" name="name" size=50> Shipping Address: <input type="text" name="ship_addr" size=50> City: <input type="text" name="ship_city" size=50> State/Province: <input type="text" name="ship_state" size=30> ZIP/Postal Code: <input type="text" name="ship_zip" size=30> Country: <input type="text" name="ship_country" size=30> Phone: <input type="text" name="phone" size=30> Email: <input type="text" name="email" size=30></pre>Payment Method:<select name="paytype"><option value="cc">Credit Card<option value="check">Check/Money Order<option>Paypal</select><br><br><input type="submit" value="Place Order"></form>EndFormprint end_html;$dbh->disconnect;sub dienice { my ($msg) = @_; print "<h2>Error</h2>\n"; print $msg; exit;}sub dbdie { my($package, $filename, $line) = caller; my($errmsg) = "Database error: $DBI::errstr<br> called from $package $filename line $line"; &dienice($errmsg
|
|
|
|