📄 00000001.htm
字号:
table is simply an index of tests so that the ID number of any test can be quickly obtained. Then we simultaneously create two new queries. The first is a CREATE TABLE query which defines our new test. The second is an INSERT query that populates our <BR>table with the maximum score for each question. These queries are then sent to the database server, completing the process (after sending a success page to the user). Later, after the students have taken the test, each student will get an entry in the <BR>test table. Then entries can then be compared to the maximum values to determine the student's score. <BR> <BR>Msql.pm <BR>The Msql.pm module is the original Perl interface to mSQL. While it has been replaced by the DBI modules, there are still many sites that depend on this old interface. To illustrate the use of Msql.pm, we will continue the teacher's aide example. <BR> <BR>Since we need classes in which to give the tests, let's examine the table of subjects. The table structure looks like this: <BR> <BR>CREATE TABLE subject ( <BR> id INT NOT NULL, <BR> name CHAR(500), <BR> teacher CHAR(100) <BR>) <BR> <BR>CREATE UNIQUE INDEX idx1 ON subject ( <BR> id, <BR> name, <BR> teacher <BR>) <BR> <BR>CREATE SEQUENCE ON subject <BR>The id number is a unique identifier for the class, while the name and teacher fields are the name of the course and the name of the teacher respectively. There is also an index of all three of the fields that speeds up queries. Finally, we define a <BR>sequence for the table. The ID numbers are generated by this sequence. <BR> <BR>The CGI program to access and manipulate this data must to several things. <BR> <BR>Search for a subject in the database. <BR>Show the subject that is the result of a search. <BR>Add a new subject to the database. <BR>Change the values of a subject in the database. <BR>With the power of Perl and mSQL, we can easily consolidate all of these functions into one file, subject.cgi. We can do this by separating each operation into its own function. The main portion of the program will be a switchboard of sorts that directs <BR>incoming requests to the proper function. We will describe the actions themselves later. <BR> <BR># Each of the different parts of the script is selected via the <BR># 'action' <BR># parameter. If no 'action' is supplied, the default() function is <BR># called. <BR># Otherwise the appropriate function is called. <BR>&default if not param('action'); <BR># This trick comes from Camel 2 and approximates the 'switch' <BR># feature of C. <BR>foreach[AO4] (param('action')) { <BR> /view/ and do { &view; last; }; <BR> /add$/ and do { &add; last; }; <BR> /add2/ and do { &add2; last; }; <BR> /add3/ and do { &add3; last; }; <BR> /add4/ and do { &add4; last; }; <BR> /schange$/ and do { &schange; last; }; <BR> /schange2/ and do { &schange2; last; }; <BR> /lchange$/ and do { &lchange; last; }; <BR> /lchange2/ and do { &lchange2; last; }; <BR> /lchange3/ and do { &lchange3; last; }; <BR> /delete/ and do { &delete; last; }; <BR> &default; <BR>} <BR>TIP: The "add," "schange," and "lchange" entries must have an anchoring "$" in the regular expression so that they do not match the other functions similar to them. Without the "$", "add" would also match add2, add3 and add4. An alternative method <BR>would be to place "add," "schange," and "lchange" after the other functions. That way they would only be called if none of the others matched. However, this method could cause trouble if other entries are added later. A third method would be to <BR>completely disambiguate all of the entries using /^view$/, /^add$/, etc. This involves slightly more typing but removes all possibility of error. <BR> <BR>Now all we have to do is fill in the details by implementing each function. <BR> <BR>The default function prints out the initial form seen by the user. This is the form that allows the user to choose which action to perform. This function is called if the CGI program is accessed without any parameters, as with <BR><A HREF="http://www.myserver.com/teach/subject.cgi,">http://www.myserver.com/teach/subject.cgi,</A> or if the ACTION parameter does not match any of the existing functions. An alternative method would be to create a function that prints out an error if the ACTION parameter is unknown. <BR> <BR>sub default { <BR> print header, start_html('title'=>'Subjects','BGCOLOR'=>'white'); <BR> print <<END_OF_HTML; <BR><h1>Subjects</h1> <BR><p>Select an action and a subject (if applicable). <BR><FORM ACTION="subject.cgi" METHOD=POST> <BR><p><SELECT NAME="action"> <BR><OPTION VALUE="view">View a Subject <BR><OPTION value="add">Add a Subject <BR><OPTION value="schange">Modify a Subject <BR><OPTION value="lchange" SELECTED>Modify a Class List <BR><OPTION value="delete">Delete a Subject <BR></select> <BR>END_OF_HTML <BR> # See 'sub print_subjects' below. <BR> &print_subjects; <BR> print <<END_OF_HTML; <BR><p> <BR><INPUT TYPE=SUBMIT VALUE=" Perform Action "> <BR><INPUT TYPE=RESET> <BR></form></body></html> <BR>HTML <BR> <BR>} <BR>There are five main actions: "view," "add," "schange" (change the information about a subject), "lchange" (change the class list for a subject), and "delete". For illustration, we will examine the "add" action in detail here. The "add" action is broken <BR>up into four separate functions because interaction with the user is required up to four times. Hidden variables are used to pass information from form to form until the class is finally created. <BR> <BR>The first add function generates the form used to enter the initial information about the class, including its name, the teacher's name, and the number of students in the class. <BR> <BR>sub add { <BR> my (%fields); <BR> foreach ('name','size','teacher') { <BR> if (param($_)) { $fields{$_} = param($_); } <BR> else { $fields{$_} = ""; } <BR> } <BR> <BR> print header, start_html('title'=>'Add a Subject','BGCOLOR'=>'white'); <BR> print <<END_OF_HTML; <BR><H1>Add a Subject</h1> <BR><form METHOD=POST ACTION="subject.cgi"> <BR><p> <BR>Subject Name: <input size=40 name="name" value="$fields{'name'}"><br> <BR>Teacher's Name: <input size=40 name="teacher" value="$fields{'teacher'}"><br> <BR>Number of Students in Class: <input size=5 name="size" value="$fields{'size'}"> <BR><p> <BR><INPUT TYPE=HIDDEN NAME="action" VALUE="add2"> <BR><INPUT TYPE=SUBMIT VALUE=" Next Page "> <BR><INPUT TYPE=RESET> <BR></form> <BR><p> <BR><A HREF="subject.cgi">Go</a> back to the main Subject page.<br> <BR><A HREF=".">Go</a> to the Teacher's Aide Home Page. <BR></body></html> <BR>END_OF_HTML <BR> <BR>} <BR>The function checks to see if any of the fields have preassigned values. This adds extra versatility to the function in that it can now be used as a template for classes with default values--perhaps generated by another CGI program somewhere. <BR> <BR>The values from the first part of the add process are passed back to CGI program into the add2 function. The first thing that add2 does is check whether the class already exists. If it does, an error message is sent to the user and he or she can change <BR>the name of the class. <BR> <BR>If the class does not already exist, the function checks how many students were entered for the class. If none were entered, the class is created without any students. The students can be added later. If the number of students was specified, the class <BR>is created and a form is displayed where the user can enter the information about each student. <BR> <BR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -