Your subroutines are constantly acting on globals. This makes it very hard to debug anything, you also need to see the entire script in that case. With mod_perl you also need to remeber that Perl is persistant, check the guide to see how this can effect you. Something along the lines of:

sub show_news { my ($dbh, $article_id) = @_; my $sth = $dbh->prepare("..."); $sth->execute($article_id); }

Would make everything much easier to debug. You don't have to worry about passing $dbh by copy as it's a reference already (a concern that seems to come up often). As you can see, the only variables the subroutine works on is those passed into it.


You need to read up on DBI a bit more.

$dbh->disconnect; $sth = $dbh->prepare(...) or die ...

Look at the above. You disconnect, thereby destructing the handle, then try to use it in the next line. If you're using Apache::DBI this may not even cause an error as it overides the $dbh->disconnect method. It's certaintly not what you meant though.

You declare RaiseError => 1 then consistantly check for errors manually anways (sort of defeats the purpose no?).

UPDATE news_articles SET article_title = ? WHERE $article_id = ?

At best this would only change the article title, not the body or any other part. And where do the variables come from? Pass them into the sub and check to make sure they're what you expect with a print statement or two.

Why do you declare connections in each sub? You're setting yourself up for errors due to inconsistancy. You many want to consider a generalised connection method.


You may want to check out CGI::Application; it's a framework that uses dispatch tables, similar to the way you seem to be setting up your scripts.


Most of these are generalised suggestions but, by taking some of them into account, I think you'll find most of your troubles go away and the ones that don't will be easier to debug.


In reply to Re: Editing data extracted from mysql through CGI/mod_perl by Arguile
in thread Editing data extracted from mysql through CGI/mod_perl by hacker

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.