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
https://forums.phpfreaks.com/topic/48391-update-on-changed-fields/
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).

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.