############################################################ # MyBoard.pm # Written and copyright 2002 by # Jacqueline D. Hamilton (kira@cgi101.com) # # This code is excerpted from "CGI Programming 201" # (http://www.cgi101.com/advanced) # # You may use this code on your own website, however # you may not publish or sell any copy or derivative work # without permission of the author. ############################################################ package MyBoard; require Exporter; @ISA = qw(Exporter); @EXPORT = qw(dienice do_header do_footer dbdie $dbh $cgi $url); @EXPORT_OK = qw(delete); use strict; use DBI; use CGI; use CGI::Carp qw(warningsToBrowser fatalsToBrowser); our $dbh = DBI->connect("dbi:mysql:cgiforum", "cgiuser", "fnord", { RaiseError => 1, AutoCommit => 1 }) or &dienice("Can't connect to database: $DBI::errstr"); # create the instance of CGI.pm our $cgi = CGI->new; # URL our $url = "http://www.cgi101.com/advanced/forums"; our $has_header = 0; sub do_header { my($page_title) = @_; if ($has_header == 0) { print $cgi->header; print $cgi->start_html(-title=>$page_title, -bgcolor=>"#ffffff", -text=>"#000000"); $has_header = 1; } } sub do_footer{ print $cgi->end_html; } sub dienice { my($msg) = @_; &do_header("Error"); print qq(

Error

\n); print qq($msg\n); &do_footer; exit; } sub dbdie { my($package, $filename, $line) = caller; my($errmsg) = "Database error: $DBI::errstr
\n called from $package $filename line $line"; &dienice($errmsg); } sub delete { my($msgid) = @_; my($sth, $rv, $f); $sth = $dbh->prepare("delete from messages where id=?") or &dbdie; $rv = $sth->execute($msgid); $sth = $dbh->prepare("select * from messages where thread_id=? limit 1") or &dbdie; $rv = $sth->execute($msgid); if ($f = $sth->fetchrow_hashref) { $dbh->do("update messages set thread_id=0 where id=$f->{'id'}") or &dbdie; $dbh->do("update messages set thread_id=$f->{'id'} where thread_id=$msgid") or &dbdie; } } 1;