Fetching the old values or using column names for dummy updates sounds like a waste of resources to me.
Since I guess that you can get the XML nodes into a hash, I would go for the simpler solution, using a dynamically built SQL statement.
Here is a (tested) example of creating a query with placeholders using a well established idiom. (See DBI recipes for more on this subject.)
# assume this is the hash containing translated XML nodes
my %hash = (
phone => '456',
street => 'Malcom St.'
);
my @fields = keys %hash;
my $ID = "34567";
my $update_query = qq{UPDATE address\n SET}
. join(",\n", map { " $_ = ? "} @fields)
. qq{\nWHERE id = ?};
print "$update_query\n";
my $sth = $dbh->prepare($update_query);
my $rows = $sth->execute(@hash{@fields}, $ID);
print "$rows rows affected\n";
__END__
The query produced by this code is
UPDATE address
SET phone = ? ,
street = ?
WHERE id = ?
Provided that the keys in your hash have a corresponding column name, you can add or remove keys at will, and this kind of code will work smoothly.
Update
Be aware that this solution won't work when the value submitted for update is a formula. E.g.
UPDATE wages SET salary = salary * 1.25 . If you pass "salary * 1.25" to a placeholder, you'll get a string, which MySQL tries to convert into a number, thus resulting in a value of "0" (zero). See the MySQL manual.
Remember that, since placeholders imply quoting, if you want to allow your users to use formulas you'll need to parse their input, check for allowed expressions, quote the quotable parts, and compose the query in a different way.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.