Dynamically Build MySQL Update String

Discussion in 'PHP' started by tanyania, Jun 17, 2011.

  1. #1
    Let's say I have a form populated with data pulled from a database. If someone overwrites one of the fields and clicks "submit", the script compares each piece of data with the original, and determines which values have changed.

    That's where I could use some help. I can see two options.

    1. Submit an update query to update all fields, even the ones that didn't change. That would make it appear that all fields were updated, and make it hard to determine what was changed, and when.

    2. Create a dynamic update query that only includes the items updated.

    I think option 2 would be best, but I am not sure how to build a dynamic update query that only includes "changed" values.

    I have looked around the web, and cannot find any answers. I probably don't know the correct verbiage to type into a search engine.

    Would someone be willing to provide me with some direction, or point me to some resources where I can find the answer myself?
     
    tanyania, Jun 17, 2011 IP
  2. militarysmurf

    militarysmurf Member

    Messages:
    93
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    40
    #2
    Do a query then compare to new and old values and then write only the 'new' values.
     
    militarysmurf, Jun 17, 2011 IP
  3. jazzcho

    jazzcho Peon

    Messages:
    326
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    jazzcho, Jun 18, 2011 IP