Jump to content

UPDATE on changed fields


cavey5

Recommended Posts

I have an html form that is populated from my database so that users can change their data. What is the correct syntax to do an UPDATE query and only change the variables that the user updated, leaving the rest alone? Do you just UPDATE * and just overwrite the unchanged variables with the same value, or is there a cleaner way to pull this off?

Link to comment
Share on other sites

UPDATE table SET field1 = 'value1', field2 = 'value2' WHERE key = 'key_value'

 

That's actually the more common update syntax, which allows natural updating of only a few fields.  Key is whatever column you use to identify a row (probably username or user_id).

Link to comment
Share on other sites

It just occurred to me.. does the user submit ALL fields, and you want to test which are different before updating?  In that situation, the safest way is to check every field individually and act appropriately.  The simplest way is to update them all.  It depends on how much effort you want to expend.

Link to comment
Share on other sites

mysql_affected_rows() just returns an integer count right? It doesn't / cannot specify which records changed? I guess if you wanted to keep the old data and new, you could append the old data with the new, and then cut the old and new apart, and display them separately.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.