📄 00000002.htm
字号:
print qq%<LI> <BR><A HREF="students.cgi?action=$subaction&id=$fields{'id'}">$fields{'first'} <BR> $fields{'middle'} $fields{'last'}%; <BR> print ", $fields{'ext'}" if $fields{'ext'}; <BR> print "\n</a>"; <BR> } <BR> } <BR> print <<END_OF_HTML; <BR></ul> <BR><p> <BR><A HREF="students.cgi?action=search">Search</a> again. <BR></body></html> <BR>END_OF_HTML <BR>} <BR>With help from the make_search_query function, this function first searches for students that match the search term. It then displays a list of the matches from which the user can select. The ID number of the selected entry is then sent to the change2 <BR>function, as shown in the following: <BR> <BR>sub change2 { <BR> my $out = $dbh->query("select * from student where id=$id"); <BR> <BR> my($did,$first,$middle,$last,$ext,$subjects,$age,$sex,$address, <BR> $city,$state,$zip,$phone) = $out->fetchrow; <BR> <BR> my @subjects = split(/:/,$subjects); <BR> shift @subjects; <BR> my $name = "$first $middle $last"; <BR> if ($ext) { $name .= ", $ext"; } <BR> <BR> print header, start_html('title'=>"$name",'BGCOLOR'=>'white'); <BR> print <<END_OF_HTML; <BR><H1>$name</h1> <BR><p> <BR><FORM ACTION="students.cgi" METHOD=POST> <BR><INPUT TYPE=HIDDEN NAME="action" VALUE="change3"> <BR><INPUT TYPE=HIDDEN NAME="id" VALUE="$id"> <BR>First: <INPUT NAME="first" VALUE="$first" SIZE=20> <BR>Middle: <INPUT NAME="middle" VALUE="$middle" SIZE=10> <BR>Last: <INPUT NAME="last" VALUE="$last" SIZE=20> <BR>Jr./III/etc.: <INPUT NAME="ext" VALUE="$ext" SIZE=5> <BR><br> <BR>Address: <INPUT NAME="address" VALUE="$address" SIZE=40><br> <BR>City: <INPUT NAME="city" VALUE="$city" SIZE=20> <BR>State: <INPUT NAME="state" VALUE="$state" SIZE=5> <BR>ZIP: <INPUT NAME="zip" VALUE="$zip" SIZE=10><br> <BR>Phone: <INPUT NAME="phone" VALUE="$phone" SIZE=15><br> <BR>Age: <INPUT NAME="age" VALUE="$age" SIZE=5> Sex: <BR>END_OF_HTML <BR> my %sexes = ( '1' => 'Male', <BR> '2' => 'Female' <BR> ); <BR> print popup_menu('name'=>'sex', <BR> 'values'=>['1','2'], <BR> 'default'=>"$sex", <BR> 'labels'=>\%sexes); <BR> print <<END_OF_HTML; <BR><p> <BR>Enrolled in:<br> <BR>END_OF_HTML <BR> my @ids = (); <BR> my %subjects = (); <BR> my $out2 = $dbh->query("select id,name from subject order by name"); <BR> while(my($id,$subject)=$out2->fetchrow) { <BR> push(@ids,$id); <BR> $subjects{"$id"} = $subject; <BR> } <BR> print scrolling_list('name'=>'subjects', <BR> 'values'=>[@ids], <BR> 'default'=>[@subjects], <BR> 'size'=>5, <BR> 'multiple'=>'true', <BR> 'labels'=>\%subjects); <BR> print <<END_OF_HTML; <BR><p> <BR><INPUT TYPE=SUBMIT VALUE=" Change Student "> <BR> <INPUT TYPE=SUBMIT NAME="delete" VALUE=" Delete Student "> <BR> <INPUT TYPE=RESET> <BR></form></body></html> <BR>END_OF_HTML <BR>} <BR>The primary purpose of this function is to print out a form very similar to the one generated from print_from. However, the values of this form must have the values of the chosen student preinserted as default values. This way, the user can edit <BR>whichever fields of the student he or she wishes without changing the rest. <BR> <BR>A couple of functions provided by the CGI.pm module come in very handy when printing form with default values. Most importantly, the function CGI::scrolling_list prints out an HTML <SELECT> block with the parameters you provide. Among other parameters, <BR>the function takes the parameters values, default, and labels which are references to the values of each <OPTION> tag, the ones which should be preselected and the labels that user sees respectively. <BR> <BR>The output of this function is a complete set of information, just as if it were coming from an add form. The difference is that the data is for a student which already exists in the database. The change3 function accepts this data and updates the <BR>student, as shown in the following: <BR> <BR>sub change3 { <BR> if (param('delete')) { &delete2($id); } <BR> else { <BR> my $query = "update student set "; <BR> my @query = (); <BR> foreach ('first', 'middle', 'last', 'ext', 'address', 'city', <BR> 'state', 'zip', 'phone') { <BR> if (param($_)) { push(@query,"$_ = ". <BR> $dbh->quote(param($_))); <BR> } <BR> } <BR> push(<A HREF="mailto:@query,"age=".param('age'))">@query,"age=".param('age'))</A> if param('age'); <BR> push(<A HREF="mailto:@query,"sex=".param('sex'))">@query,"sex=".param('sex'))</A> if param('sex'); <BR> <BR> my $subjects = "':"; <BR> $subjects .= join(":",param('subjects')); <BR> $subjects .= ":" unless $subjects eq "':"; <BR> $subjects .= "'"; <BR> push(@query,"subjects=$subjects"); <BR> <BR> $query .= join(", ",@query) . " where id=$id"; <BR> $dbh->query($query); <BR> <BR> print header, start_html('title'=>'Student Changed', <BR> 'BGCOLOR'=>'white'); <BR> # Print success form <BR> ... <BR> } <BR>} <BR>Note that if the user chose the "Delete" button on the change page, this function automatically passes the ball to the delete function. This is one major advantage of integrating several functions into one program. If no user interaction is required, <BR>you can skip from function to function without sending redirect messages to the user. <BR> <BR>The rest of this function is fairly straightforward. The information about the student is gathered into an UPDATE query, which is sent to the MySQL server. A success page is then sent to the user. <BR> <BR> <BR>-------------------------------------------------------------------------------- <BR> <BR>1. This example is a MySQL example. Of course, the API is identical for mSQL. The only "glitch" is with sequence generation. Remember that where MySQL automatically generates the next ID for the test table because of the AUTO_INCREMENT keyword, mSQL <BR>expects you to create a sequence on the test table and SELECT the _seq value before doing your insert. <BR> <BR>Back to: MySQL & mSQL <BR> <BR> <BR> <BR>-------------------------------------------------------------------------------- <BR>O'Reilly Home | O'Reilly Bookstores | How to Order | O'Reilly Contacts <BR>International | About O'Reilly | Affiliated Companies <BR>? 1999, O'Reilly & Associates, Inc. <BR> <BR> <BR>-- <BR>※ 来源:·BBS 水木清华站 bbs.net.tsinghua.edu.cn·[FROM: 162.105.17.153] <BR><CENTER><H1>BBS水木清华站∶精华区</H1></CENTER></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -