Thank you, Masem
I've been updating my code, taking yours and others' suggestions into account, and have come up with this:
my $action = param("ACTION") || "NONESUCH"; #Bond the built-in and accessory components to the parent. if($action eq "BOND") { my $main = param("MAIN"); my @built = param("BUILT"); my @acc = param("ACC"); my $query = "SELECT location FROM demstock2 WHERE ds_id=?"; my $sth = $db->prepare($query); $sth->execute($main) or print "Can't execute <pre>$query</pre>: " . $db->er +rstr . "<br>\n"; my $mainlocation = $sth->fetchrow_hashref; $sth->finish(); if($mainlocation->{"location"} ne "STOCK") { print b("Refusing to bond items to $main, as it isn't +in free stock") . br(); } else { for my $child (@built, @acc) { #Find out if it still is in free stock my $query = "SELECT curr_parent_id FROM demsto +ck2 WHERE ds_id=?"; my $sth = $db->prepare($query); $sth->execute($child) or print "Can't execute $query: " . $s +th->errstr . "<br>\n"; my $current_status = $sth->fetchrow_hashref; $sth->finish(); if($current_status->{"curr_parent_id"} != 0) { print "Refusing to modify the status o +f demstock $child. It has already been moved<br>\n"; next; } #Move the kit, and update the action table my $move_query = "UPDATE demstock2 SET curr_pa +rent_id=? WHERE ds_id=?"; my $move_sth = $db->prepare($move_query); $move_sth->execute($main, $child) or print "Can't execute <PRE>$move_que +ry</PRE>: " . $sth->errstr . br(); $move_sth->finish(); my $action_query = "INSERT INTO action "; $action_query .= "(action_id, action_type, d +s_id, occurred, ref_no, staff_id) VALUES"; $action_query .= "(NULL, 'MOVE', ? +, CURRENT_TIMESTAMP, ?, ?)"; my $action_sth = $db->prepare($action_query) +; $action_sth->execute($child, "0,$main", $calus +er) or print "Can't execute <PRE>$action_q +uery</PRE>: " . $sth->errstr . br(); $action_sth->finish; } } }
Which, IMHO, is much more easy to read. (Notice the removal of the second for() loop, as the two loops simply did the same thing. Style is not a replacement for sensible coding :-)
I've renamed some of the query variables as per tommyw's suggestions, and I've also used placeholders (which evidently rock). A quick note on the security: this is actually for internal company use only, so hopefully nobody's going to try and break it, hence the specific error messages. However, I see your point about security, and the fact that it's internal doesn't mean it shouldn't be bulletproof.

About the if{} logic placement - that's something I nabbed from "Practical C Programming" - keep the shorter pieces of code (like error messages) near the top of the if's, so that it's easier to see which error message is the alternative to which bit of code.
Like you say though, this is personal preference, and feel free to ignore it :)
Thanks again to everyone who replied.
davis

In reply to Re: Re: A question of style. by davis
in thread A question of style. by davis

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.