Since
andyford has given the advice that is directly relevant, let me give you some slightly off-topic advice:
- Don't use "!=" to test inequality of string values; use "ne" for that.
- Use "my" to declare each variable close to (just within the scope of) the place where you actual use it; declaring them all at the top is crude and not helpful.
- You might want to check all your parameters for valid, sensible values (not just the date field).
- Your SQL statements should be prepared with "?" placeholders where the parameter values go, and the parameters should be passed as args in the "execute" calls.
- If/when you turn off the "fatals to browser" feature, you should make your error messages more informative -- include $0 (name of the script) and the full sql statement or whatever made it die -- so that referring to the web server's error log will be more helpful.
- If you use a hash for parameters (keys are param names, values are param values) instead of a distinct scalar variable for every distinct param, the code will be easier to maintain.
- When you find yourself copy/pasting the same code block lots of times and just changing a variable name each time, you really should consider turning it into a subroutine and/or using a loop over the variables (this is where a hash can be really handy), so that the code block only needs to occur once in your script. This will also make your code a lot easier to maintain (and easier for others to read and understand).
Here's a version of your code that applies some of those suggestions. It should be equivalent to what you posted (but I only tested to make sure that it compiles correctly).
#!/user/bin/perl -w
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);
use strict;
use DBI;
print "content-type: text/html\n\n";
my @parnames = qw/Update Name Region Region_Section POS NPC
Requirements Requiredquests Fame Reward
Rewardtype Title isrepeatable walkthrough date/;
my @to_update = @parnames[2..$#parnames];
my @to_insert = @parnames[1..$#parnames];
my %col_name;
my %par_value;
for my $par ( @parnames ) {
$col_name{$par} = lc( $par );
$par_value{$par} = param( $par ) || '';
}
# handle some exceptional column names:
$col_name{Region_Section} = 'region_region';
$col_name{Name} = 'quest_name';
$col_name{date} = 'updated';
my $sitepath = 'e:/web/public_html/finalfantasyinfo';
my $dbh = DBI -> connect ('dbi:ODBC:', '', '') or die "$DBI::errstr;";
if ($par_value{Update} eq 'on') {
for my $par ( @to_update ) {
next if ( $par_value{$par} eq 'No Update' );
my $sql = "update quests set $col_name{$par} = ? where quest_n
+ame = ?";
my $sth = $dbh->prepare( $sql );
$sth->execute( $par_value{$par}, $par_value{Name} )
or die "$sql:\n $DBI::errstr";
}
}
else {
my $cols = join( ",", @to_insert );
my $vals = join( ",", ("?") x scalar @to_insert );
my $sql = "insert into quests ($cols) values ($vals)";
my $sth = $dbh->prepare( $sql );
$sth->execute( @par_value{@to_insert} )
or die "$sql:\n $DBI::errstr";
}
$dbh -> disconnect();
print "Looks like that worked.";
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.