📄 00000001.htm
字号:
<HTML><HEAD> <TITLE>BBS水木清华站∶精华区</TITLE></HEAD><BODY><CENTER><H1>BBS水木清华站∶精华区</H1></CENTER>发信人: yyh (阿欢&正在努力上进中......), 信区: Linux <BR>标 题: MySQL & mSQL Chapter 10 Perl(2) <BR>发信站: BBS 水木清华站 (Sat Sep 18 01:43:47 1999) <BR> <BR>This function displays a form allowing the user to choose a subject for the test along with the number of questions and a name. In order to print out a list of available subjects, the table of subjects is queried. When using a SELECT query with DBI, the <BR>query must first be prepared and then executed. The DBI::prepare function is useful with certain database servers which allow you to perform operations on prepared queries before executing them. With MySQL and mSQL however, it simply stores the query <BR>until the DBI::execute function is called. <BR> <BR>The output of this function is sent to the add2 function as shown in the following: <BR> <BR>sub add2 { <BR> my $subject = param('subjects'); <BR> my $num = param('num'); <BR> $name = param('name') if param('name'); <BR> <BR> my $out = $dbh->prepare("select name from subject where id=$subject"); <BR> $out->execute; <BR> my ($subname) = $out->fetchrow_array; <BR> <BR> print header, start_html('title'=>"Creating test for $subname", <BR> 'BGCOLOR'=>'white'); <BR> print <<END_OF_HTML; <BR><H1>Creating test for $subname</h1> <BR><h2>$name</h2> <BR><p> <BR><FORM ACTION="test.cgi" METHOD=POST> <BR><INPUT TYPE=HIDDEN NAME="action" VALUE="add3"> <BR><INPUT TYPE=HIDDEN NAME="subjects" VALUE="$subject"> <BR><INPUT TYPE=HIDDEN NAME="num" VALUE="$num"> <BR><INPUT TYPE=HIDDEN NAME="name" VALUE="$name"> <BR>Enter the point value for each of the questions. The points need not <BR>add up to 100. <BR><p> <BR>END_OF_HTML <BR> for (1..$num) { <BR> print qq%$_: <INPUT NAME="q$_" SIZE=3> %; <BR> if (not $_ % 5) { print "<br>\n"; } <BR> } <BR> print <<END_OF_HTML; <BR><p> <BR>Enter the text of the test:<br> <BR><TEXTAREA NAME="test" ROWS=20 COLS=60> <BR></textarea> <BR><p> <BR><INPUT TYPE=SUBMIT VALUE="Enter Test"> <BR> <INPUT TYPE=RESET> <BR></form></body></html> <BR>END_OF_HTML <BR>} <BR>In this function, a form for the test is dynamically generated based on the parameters entered in the last form. The user can enter the point value for each question on the test and the full text of the test as well. The output of this function is then <BR>sent to the final function, add3, as shown in the following: <BR> <BR>sub add3 { <BR> my $subject = param('subjects'); <BR> my $num = param('num'); <BR> <BR> $name = param('name') if param('name'); <BR> <BR> my $qname; <BR> ($qname = $name) =~ s/'/\\'/g; <BR> my $q1 = "insert into test (id, name, subject, num) values ( <BR> '', '$qname', $subject, $num)"; <BR> <BR> <BR> <BR> my $in = $dbh->prepare($q1); <BR> $in->execute; <BR> <BR> # Retrieve the ID value MySQL created for us <BR> my $id = $in->insertid; <BR> <BR> my $query = "create table t$id ( <BR> id INT NOT NULL, <BR> "; <BR> <BR> my $def = "insert into t$id values ( 0, "; <BR> <BR> my $total = 0; <BR> my @qs = grep(/^q\d+$/,param); <BR> foreach (@qs) { <BR> $query .= $_ . " INT,\n"; <BR> my $value = 0; <BR> $value = param($_) if param($_); <BR> $def .= "$value, "; <BR> $total += $value; <BR> } <BR> $query .= "total INT\n)"; <BR> $def .= "$total)"; <BR> <BR> my $in2 = $dbh->prepare($query); <BR> $in2->execute; <BR> my $in3 = $dbh->prepare($def); <BR> $in3->execute; <BR> <BR> # Note that we store the tests in separate files. This is <BR> # useful when dealing with mSQL because of its lack of BLOBs. <BR> # (The TEXT type provided with mSQL 2 would work, but <BR> # inefficently.) <BR> # Since we are using MySQL, we could just as well <BR> # stick the entire test into a BLOB. <BR> open(TEST,">teach/tests/$id") or die("A: $id $!"); <BR> print TEST param('test'), "\n"; <BR> close TEST; <BR> <BR> print header, start_html('title'=>'Test Created', <BR> 'BGCOLOR'=>'white'); <BR> print <<END_OF_HTML; <BR><H1>Test Created</h1> <BR><p> <BR>The test has been created. <BR><p> <BR><A HREF=".">Go</a> to the Teacher's Aide home page.<br> <BR><A HREF="test.cgi">Go</a> to the Test main page.<br> <BR><A HREF="test.cgi?action=add">Add</a> another test. <BR></body></html> <BR>END_OF_HTML <BR>} <BR>Here we enter the information about the test into the database. In doing so we take a step beyond the usual data insertion that we have seen so far. The information about the test is so complex that each test is best kept in a table of its own. <BR>Therefore, instead of adding data to an existing table, we have to create a whole new table for each test. First we create an ID for the new test using MySQL auto increment feature and enter the name and ID of the test into a table called test. This <BR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -