################################################# # The usual suspects use strict; use warnings; use diagnostics; use Env; # Send the errors to the browser use CGI::Carp qw( fatalsToBrowser ); use CGI qw(:standard); use CGI qw(:standard start_div end_div); use DBI; my ($script, # this script $dbuser, # database auth user $dbpass, # password to connect to db $dbh, # database handle $sth, # select handle $article_arrayref, # reference to the array from DBI $article_row, # the result from the query $article_id, # id number of the article $article_title, # table article_title $article_summary, # short synopsis of the article $article_date, # date the article was posted $article_author, # author of the article $article_body, # table article_body @row # array holding result ); &print_header(); &top_menubar(); if (!$vars{action} || $vars{action} eq "home") { &left_content(); &right_content(); } if ($vars{action} && $vars{action} eq "news") { &news if (!$vars{article}); &show_news if ($vars{article} || $vars{editnews}); } if ($vars{action} && $vars{action} eq "editnews") { &edit_news($article_title, $article_body); } &bottom_menubar(); &end_page(); ################################################# # # Pull articles from the db # ################################################# sub show_news { $dbh = DBI->connect('DBI:mysql:plucker:localhost:3306', $dbuser, $dbpass, {RaiseError => 1}); $sth = $dbh->prepare ("SELECT article_id, article_title, article_author, DATE_FORMAT(article_date, '%W %M %D %Y') as my_date, article_body from news_articles where article_id=?"); $sth->execute($vars{article}); $article_arrayref = $sth->fetchall_arrayref() or die "[$DBI::err] $DBI::errstr"; foreach $article_row (@{$article_arrayref}) { ($article_id, $article_title, $article_author, $article_date, $article_body) = @{$article_row}; print div({-id=>'newsbody'}, h3("$article_title"), p({-class=>'fol'}, a({-href=>"$script?action=editnews&article=$article_id"}, "Edit this article")), p("$article_author"), i("$article_date"), "$article_body"); } $sth->finish; $dbh->disconnect; } ################################################# # # Show the news articles in the edit window # ################################################# sub edit_news { $dbh = DBI->connect('DBI:mysql:plucker:localhost:3306', $dbuser, $dbpass, {RaiseError => 1}); $sth = $dbh->prepare ("UPDATE news_articles SET article_title = ? WHERE $article_id = ?") or die $dbh->errstr; $sth->execute($article_title, $article_id) or die die $dbh->errstr; $sth->finish; $dbh->disconnect; $sth = $dbh->prepare ("SELECT article_id, article_title, article_author, article_body from news_articles where article_id like $vars{article}"); $sth->execute or die "Can't execute!"; $article_arrayref = $sth->fetchall_arrayref() or die "[$DBI::err] $DBI::errstr"; foreach $article_row (@{$article_arrayref}) { ($article_id, $article_title, $article_author, $article_body) = @{$article_row}; print start_form(-action=>"$script?action=editnews&article=$article_id"), div({-id=>'newsbody'}, p("Enter a new article title:"), textfield({-name=>'newstitle',-size=>60, -value=>"$article_title"}), p("Enter a new article author:"), textfield({-name=>'newsauthor',-size=>60, -value=>"$article_author"}), p("Edit additional story details"), textarea({-name=>'newsbody',-cols=>60, -rows=>30, -value=>"$article_body"}), submit({-name=>'editnews', -value=>'Submit edited changes'}) ); } $sth->finish; $dbh->disconnect; }