Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re: How to get a placeholder to keep the current column value?

by BUU (Prior)
on Oct 26, 2003 at 20:17 UTC ( #302256=note: print w/replies, xml ) Need Help??

in reply to How to get a placeholder to keep the current column value?

This is a tad late and perhaps superfluous, but why are you bothering with all this dynamic sql and so on? When you execute an update in mysql it only changes the columns you specify, yes? So just leave all the other fields blank and let the database handle it. Eg, assuming you have a hash with the key being the column name and the value being the, uh, value, then just do something like:
my $sql='UPDATE table SET '.join',',map {"$_=".quote($hash{$_})} keys +%hash;
Then, in your example, you just end up with sql looking like:
UPDATE table SET phone='456'
Obviously you need a where statement and so on, but thats simple. This way you only touch the fields you want to change and you don't need to worry about restoring the old values and fun stuff like that. The only problem I see is if they wanted to submit say, an empty field and not update the value which would probably require something slightly more complicated then a nice map, but perhaps you could chain maps:
map {"$_=".quote($hash{$_})} grep defined $hash{$_},keys %hash;

Replies are listed 'Best First'.
Re: Re: How to get a placeholder to keep the current column value?
by castaway (Parson) on Oct 26, 2003 at 20:48 UTC
    Pardon me for wondering, but, isn't this dynamic SQL, that you're proposing? ;)

    Dynamic = produced on the fly, not static.


      Well, ok, perhaps I missused the term or over used it or something of that nature. I really meant however, that it seemed much simpler to just touch the fields you want and ignore the rest, letting the database do it for you rather then trying to find all the old values and so on yourself. Dynamic sql just kind of slipped in and out there =].

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://302256]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (2)
As of 2018-04-21 00:06 GMT
Find Nodes?
    Voting Booth?