CGI プログラム1

戻る

#テーブルのすべての内容を表示 sub mscshowall { my $self = shift; my(%t,$template,$n,@arr,@arr1,@loop); # Get CGI query object $t{q} = $self->query(); # データベースにアクセス $t{table} = $t{q}->param("table"); $t{item} = $t{q}->param("items"); @arr = split(/,/,$t{item}); $t{items} = $arr[0]; for $n ( 1 .. $#arr ) { $t{items} = $t{items} . ',' . $arr[$n]; } @loop = (); $t{sth} = $self->dbh->prepare("SELECT $t{items} FROM $t{table}"); $t{sth}->execute; while ( @arr1 = $t{sth}->fetchrow_array ) { my $row_ref = (); # この初期化はとても重要! for $n ( 0 .. $#arr ) { $t{name} = $arr[$n]; $t{value} = $arr1[$n]; $$row_ref{$t{name}} = $t{value}; } push(@loop, $row_ref); } $t{sth}->finish; # HTMLファイルに出力 $t{html} = $t{q}->param("tmpl"); $t{html} = $t{html} . '.htm'; $template = $self->load_tmpl("$t{html}") || die "error loading tmpl"; $template->param(THIS_LOOP => \@loop); return $template->output; } 1; -------------------------------------------------------------------------- テーブル項目追加 sub mscinsert { my $self = shift; my(%t,$q,$template,$id,$items,@arr,@arr1,$n,$sql,$insert); # Get CGI query object $q = $self->query(); $t{html} = $q->param("tmpl"); $t{html} = $t{html} . '.htm'; $template = $self->load_tmpl("$t{html}") || die "error loading tmpl"; $id = $q->param("id"); $table = $q->param("table"); $items = $q->param("items"); @arr = split(/,/,$items); @arr1 = (); for $n ( 0 .. $#arr ) { $t{name} = $arr[$n]; $t{one} = $q->param($t{name}); push(@arr1,$t{one}); } $sql = "INSERT INTO $table ("; for $n ( 0 .. ($#arr-1) ) { $sql .= "$arr[$n],"; } $sql .= "$arr[$#arr]) VALUES("; for $n ( 0 .. ($#arr1-1) ) { $sql .= "'" . $arr1[$n] . "',"; } $sql .= "'" . "$arr1[$#arr1]')"; $insert = $self->dbh->do("$sql"); $template->param(id => $id); for $n ( 0 .. $#arr ) { $t{name} = $arr[$n]; $t{value} = $arr1[$n]; $template->param($t{name} => $t{value}); } return $template->output; } 1; DBD::mysql::db do failed: Data too long for column 'memo' at row 1 at ./pro/mscinsert.pl line 40, "my" variable $q masks earlier declaration in same scope at ./pro/mscinsert.pl line 12. -------------------------------------------------------------------------- テーブル項目修正 sub mscupdate { my $self = shift; my(%t,$q,$template,$id,$items,@arr,@arr1,$n,$sql,$update); # Get CGI query object $q = $self->query(); $t{html} = $q->param("tmpl"); $t{html} = $t{html} . '.htm'; $template = $self->load_tmpl("$t{html}") || die "error loading tmpl"; $id = $q->param("id"); $table = $q->param("table"); $items = $q->param("items"); @arr = split(/,/,$items); @arr1 = (); for $n ( 0 .. $#arr ) { $t{name} = $arr[$n]; $t{one} = $q->param($t{name}); push(@arr1,$t{one}); } $sql = "UPDATE $table SET "; for $n ( 0 .. ($#arr1-1) ) { $sql .= "$arr[$n]='" . $arr1[$n] . "',"; } $sql .= "$arr[$#arr1]='" . $arr1[$#arr1] . "' "; $sql .= "where id =" . $id; $update = $self->dbh->do("$sql"); $template->param(id => $id); for $n ( 0 .. $#arr ) { $t{name} = $arr[$n]; $t{value} = $arr1[$n]; $template->param($t{name} => $t{value}); } return $template->output; } 1; ?V?X?e???G???[???N???܂????F HTML::Template : Attempt to set nonexistent parameter 'company' - this parameter name doesn't match any declarations in the template file : (die_on_bad_params => 1) at ./pro/mscupdate.pl line 42 -------------------------------------------------------------------------- #テーブルの一行項目を修正 sub mscshowadd { my $self = shift; my(%t,$q,$template,$table,$id,@arr); # Get CGI query object $q = $self->query(); $t{html} = $q->param("tmpl"); $t{html} = $t{html} . '.htm'; $template = $self->load_tmpl("$t{html}") || die "error loading tmpl"; $table = $q->param("table"); @arr = $self->dbh->selectrow_array("SELECT MAX(id) FROM $table"); $id = $arr[0] + 1; $template->param(id => $id); return $template->output; } 1; -------------------------------------------------------------------------- #テーブルの一行項目を修正 sub mscshowone { my $self = shift; my(%t,$q,$template,$id,$items,@arr,@arr1,$n); # Get CGI query object $q = $self->query(); $t{html} = $q->param("tmpl"); $t{html} = $t{html} . '.htm'; $template = $self->load_tmpl("$t{html}") || die "error loading tmpl"; $id = $q->param("id"); $table = $q->param("table"); $items = $q->param("items"); @arr = split(/,/,$items); $template->param(id => $id); $t{items} = $arr[0]; for $n ( 1 .. $#arr ) { $t{items} = $t{items} . ',' . $arr[$n]; } @arr1 = (); @arr1 = $self->dbh->selectrow_array("SELECT $t{items} FROM makers WHERE id = $id"); for $n ( 0 .. $#arr ) { $t{name} = $arr[$n]; $t{value} = $arr1[$n]; $template->param($t{name} => $t{value}); } return $template->output; } 1; -------------------------------------------------------------------------- $template = $self->load_tmpl('$t{html}') || die "error loading tmpl"; ?V?X?e???G???[???N????????F HTML::Template->new() : Cannot open included file $t{html} : file not found. at C:/Perl/site/lib/HTML/Template.pm line 1658 HTML::Template::_init_template('HTML::Template=HASH(0x1a0f640)') called at C:/Perl/site/lib/HTML/Template.pm line 1238 HTML::Template::_init ==>$template = $self->load_tmpl("$t{html}") || die "error loading tmpl"; -------------------------------------------------------------------------- Premature end of script headers: msc_start.cgi, referer: http://localhost/cgi-bin/msc_start.cgi No such run mode 'msc232' at C:/www/cgi-bin/msc_start.cgi line 6, referer: http://localhost/cgi-bin/msc_start.cgi ==>msc232ではなく,mode232です。
戻る