hacker has asked for the wisdom of the Perl Monks concerning the following question:
I've been trying to figure out the architecture behind querying articles from a mysqldb, populating them in a form which allows editing, then submitting those changes back to the database. I've got all the parts working, except that final UPDATE. I'm not entirely sure my logic is sound here.
I can pull the articles from the db, display them, populate the edit form with the values, but when I change the values in the form and submit, the data in the database is never updated. The form action is specified to be:
Ideally, this should allow me to edit the contents of article 15 from the database, and submit it back with updated changes.<form action=$script?action=editnews&amp;amp;amp;article=15 enctype="application/x-www-form-urlencoded">
Here's the relevant code. I'm still a bit confused as I try to debug Apache::Registry errors and warnings, mysql errors and warnings, and mod_perl/perl errors and warnings, so there may be something very fundamental I'm missing and just haven't caught it yet:
################################################# # 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_au +thor, 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 = ? W +HERE $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_au +thor, article_body from news_articles where articl +e_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=$ar +ticle_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; }
Edit kudra, 2002-06-10 added readmore
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Editing data extracted from mysql through CGI/mod_perl
by talexb (Chancellor) on Jun 10, 2002 at 17:34 UTC | |
|
Re: Editing data extracted from mysql through CGI/mod_perl
by perrin (Chancellor) on Jun 10, 2002 at 18:53 UTC | |
|
Re: Editing data extracted from mysql through CGI/mod_perl
by Arguile (Hermit) on Jun 10, 2002 at 18:59 UTC | |
by hacker (Priest) on Jun 10, 2002 at 20:03 UTC | |
by Arguile (Hermit) on Jun 10, 2002 at 21:07 UTC |