📄 00000001.htm
字号:
sub add2 { <BR> ... <BR> my $name = param('name'); <BR> # We need one copy of the name that is encoded for the URL. <BR> my $enc_name = &cgi_encode($name); <BR> # We also need a copy of the name that is quoted safely for insertion <BR> # into the database. Msql provides the Msql::quote() function for that <BR> # purpose. <BR> my $query_name = $dbh->quote($name); <BR> <BR> # We now build a query to see if the subject entered already exists. <BR> my $query = <BR> "select id, name, teacher from subject where name=$query_name"; <BR> <BR> # If the user supplied a teacher's name, we check for that teacher <BR> # specifically, since there can be two courses with the same name but <BR> # different teachers. <BR> if (param('teacher')) { <BR> $teacher = param('teacher'); <BR> $enc_teacher = &cgi_encode($teacher); <BR> my $query_teacher = $dbh->quote($teacher); <BR> $query .= " and teacher=$query_teacher"; <BR> } <BR> <BR> # Now we send the query to the mSQL server. <BR> my $out = $dbh->query($query); <BR> # We check $out->numrows to see if any rows were returned. If <BR> # there were any, and the user didn't supply an 'override' <BR> # parameter, then we exit with a message that the class already <BR> # exists, and giving the user a change to enter the class anyway <BR> # (by resubmitting the form with the 'override' parameter set. <BR> if ($out->numrows and not param('override')) { <BR> # Print 'Class already exists' page. <BR> ... <BR> } else { <BR> # Now we enter the information into the database. <BR> # First, we need to select the next number from the <BR> # table's sequence. <BR> $out = $dbh->query("select _seq from subject"); <BR> my ($id) = $out->fetchrow; <BR> <BR> # Then we insert the information into the database, using <BR> # the sequence number we just obtained as the ID. <BR> $query = "INSERT INTO subject (id, name, teacher) <BR> VALUES ($id, '$name', '$teacher')"; <BR> $dbh->query($query); <BR> <BR> # If the user did not specify a class size, we exit with <BR> # a message letting the user know that he or she can add <BR> # students later. <BR> if (not param('size')) { <BR> # Print success page. <BR> ... <BR> } else { <BR> # Now we print a form, allowing the user to enter the <BR> # names of each of the students in the class. <BR> print header, start_html('title'=>'Create Class List', <BR> 'BGCOLOR'=>'white'); <BR> print <<END_OF_HTML; <BR><H1>Create Class List</h1> <BR><P> <BR><B>$name</b> has been added to the database. You can <BR>now enter the names of the students in the class. <BR>You may add or drop students later from the <BR><a href="subject.cgi">main <BR>Subject page</a>. <BR><p> <BR><FORM METHOD=POST ACTION="subject.cgi"> <BR><INPUT TYPE=HIDDEN NAME="action" VALUE="add3"> <BR><INPUT TYPE=HIDDEN NAME="id" VALUE="$id"> <BR><TABLE BORDER=0> <BR><TR><TH><TH>First Name<TH>Middle Name/Initial <BR><TH>Last Name<TH>Jr.,Sr.,III,etc <BR></tr> <BR>END_OF_HTML <BR> for $i (1..$size) { <BR> print <<END_OF_HTML; <BR><TR><TD>$i<TD><INPUT SIZE=15 NAME="first$i"><TD><INPUT SIZE=15 <BR>NAME="middle$i"> <BR> <TD><INPUT SIZE=15 NAME="last$i"><TD><INPUT SIZE=5 <BR>NAME="ext$i"></tr> <BR>END_OF_HTML <BR> <BR> } <BR> print <<END_OF_HTML; <BR></table> <BR><INPUT TYPE=SUBMIT VALUE=" Submit Class List "> <BR><INPUT TYPE=RESET> <BR></form></body></html> <BR>END_OF_HTML <BR> <BR> } <BR> } <BR>} <BR>Note that the function used three copies of the name parameter. To use a variable as part of a URL, all special characters must be URL-escaped. A function called cgi_encode is provided with the code for this example which performs this operation. <BR>Secondly, to insert a string into the mSQL database, certain characters must be escaped. The MsqlPerl interface provides the function quote--accessible through any database handle--to do this. Finally, an unescaped version of the variable is used when <BR>displaying output to the user. <BR> <BR>When adding the class to the database, mSQL's sequence feature comes in handy. Remember that a sequence was defined on the class table. The values of this sequence are used as the unique identifiers for each class. In this way two classes can have the <BR>same name (or same teacher, etc.) and still be distinct. This also comes in handy when modifying the class later. As long as the unique ID is passed from form to form, any other information about the class can safely be changed. <BR> <BR>Finally, notice that the student entry form displayed by this function is dynamically generated. The number of students entered for the class is used to print out a form with exactly the right number of entries. Always remember that the CGI program has <BR>complete control over the generated HTML. Any part, including the forms, can be programmatically created. <BR> <BR>If the user did not enter any students for the class, we are now finished. The user can use the change feature to add students later. However, if students were requested, the information about those students is passed onto the stage in the add3 <BR>function, as shown in the following: <BR> <BR>sub add3 { <BR> if (not param('id')) { &end("An ID number is required"); } <BR> my $id = param('id'); <BR> <BR> my @list = &find_last_student; <BR> my ($ref_students,$ref_notstudents) = <BR>&find_matching_students(@list); <BR> <BR> @students = @$ref_students if $ref_students; <BR> @notstudents = @$ref_notstudents if $ref_notstudents; <BR> <BR> if (@notstudents) { <BR> # Print form telling the user that there are nonexisting <BR> # students in the list. The user can then automatically create <BR> # the students or go back and fix any typos. <BR> ... <BR> } else { <BR> &update_students($id,@students); <BR> # Print success page. <BR> ... <BR> } <BR>} <BR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -