מסדי נתונים / בחירת מסד הנתונים האידיאלי / מעבדים את תוצאות הטופס עם - CGI.pm
מעבדים את תוצאות הטופס עם - CGI.pm receive.iphtm
<?
my $cgi = new CGI; # to take advantage of the "param"
decoding method
my $dbh = DBI->connect('DBI:mysql:test:localhost', '','',
{ PrintError => 0}) || die $DBI::errstr;
!>//
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<HTML>
<HEAD>
<TITLE>Results from the Database Search</TITLE>
</HEAD>
<BODY>
<P>
<?=${\( display_query_results(\$dbh, \$cgi) )}!>//
<P>
<A HREF=selection.iphtml>Return to the query page</A>
</BODY>
</HTML>
<?
$dbh->disconnect;
!>//
<?
sub display_query_results {
#
# Note that I passed references to the database handle dbh and the
# cgi object - this means that in order to reference them in this
# subroutine, I'll have to refer to them with $$dbh and $$cgi.
#
my $dbh = shift;
my $cgi = shift;
my $return = '';
my $SQL;
if ( defined($$cgi->param('title')) ) {
my $SQL = <<"EOT";
select title, artist, year(released)
from albums
where ucase(title) like ${ \($$dbh->quote(uc($$cgi->param('title')) .
'%') ) }
order by title, artist
EOT
#
# I use more complicated SQL in the above statement. SQL won't
# automatically order the returned results, but it's very easy
to do so with the
# "order by" statement - just specify the columns you want
to appear
# and their order of priority. "Like" allows me to match on
substrings -
# if you provide the title "abc," then all albums titles beginning
with "abc"
# will be returned. In order to make this comparison
case-insensitive, I
# use ucase(title) in SQL, and uc($$cgi->param('title'), to
put both strings
# into the upper case. The % character is a wildcard, much like *
in Unix
# file name globbing.
#
my $cursor = $$dbh->prepare($SQL);
$cursor->execute;
$return .= "<TABLE BORDER>\n<TR><TH COLSPAN=3>";
$return .= "<B>Matches on the title search for: <TT><I>${
\($$cgi->param('title') )}</I></TT></B></TH></TR>";
$return .= "<TR><TH>Title</TH><TH>Artist</TH>
<TH>Year of Release</TH></TR>\n";
my @fields;
while ( @fields = $cursor->fetchrow ) {
$return .= "<TR><TD>$fields[0]</TD><TD>$fields[1]
</TD><TD>$fields[2]</TD></TR>\n";
}
$cursor->finish;
$return .= "</TABLE>\n";
} else {
if ( defined($$cgi->param('artist_submit')) ) {
$SQL = <<"EOT";
select title, year(released)
from albums
where artist = ${ \($$dbh->quote($$cgi->param('artist'))) }
order by released desc, title
EOT
} elsif ( defined($$cgi->param('year_submit')) ) {
$SQL = <<"EOT";
select artist, title
from albums
where year(released) = ${ \($$dbh->quote($$cgi->param('year'))) }
order by artist, title
EOT
}
my $cursor = $$dbh->prepare($SQL);
$cursor->execute;
$return .= "<TABLE BORDER>\n<TR><TH COLSPAN=2>";
$return .= (defined($$cgi->param('artist_submit'))?
("<B>Artist: <TT><I>".
"${ \($$cgi->param('artist')) }</I>".
"</TT></B></TH></TR>\n<TR>".
"<TH>Album Title</TH>".
"<TH>Year of Release</TH></TR>\n"):
("<B>Year of Release: <TT><I>".
"${ \($$cgi->param('year')) }</I>".
"</TT></B></TH></TR>\n<TR>".
"<TH>Artist</TH><TH>Album Title</TH>".
"</TR>\n"));
my @fields;
while ( @fields = $cursor->fetchrow ) {
$return .= "<TR><TD>$fields[0]</TD>";
$return .= "<TD>$fields[1]</TD></TR>\n";
}
$cursor->finish;
$return .= "</TABLE>\n";
}
$return;
}
!>//
זה הכל חברים!
וכאן נגמר השיעור. אבל האמת, זו רק ההתחלה. מסדי נתונים באינטרנט ייקחו אותך ואת משתמשי האתר שלך רחוק מאוד.
אני מקווה שהסדרה הזו הייתה מועילה וכיפית, וכמובן, נקודת התחלה.
|