0

I have a edit profile page in my social media website.

When users click submit on the form. I run an update query to obviously update the users field in the database.

How can I optimize this scenario to include the logging of which particular fields are updated?

So for e.g.

One scenario could be:

Chris updated his profile picture.

Another scenario would be:

Chris updated his profile, inc:

  • Email
  • Username
  • Address
  • Address 2

Can anyone offer a solution to this?

I feel there is no need for code as all it is, is an update query.

Thanks

cwiggo
  • 2,541
  • 9
  • 44
  • 87

3 Answers3

1

When writing out the form, save the current states in the $_SESSION-variable. The check the submitted forms and compare with the data in the $_SESSION-variable. Then only make an update on the forms that have changed.

if($_SESSION['name'] != $myform['name']) { $sql[] = "name = '{$myform['name']}'"; }
if($_SESSION['img'] != $myform['img']) { $sql[] = "img = '{$myform['img']}'"; }
$sqlstring = "UPDATE mytable SET " . implode(",",$sql);
// run the sql

EDIT: to implement logging:

// populate the variables (name, img) from the db/session with the highest revision number.
// ie SELECT * FROM mytable where userid = $userid ORDER BY revision DESC LIMIT 1
$revision = $_SESSION['revision'] + 1;
$sql = "INSERT INTO mytable SET img = '$img', name='$name', revision='$revision'"; 
Hampus Brynolf
  • 1,296
  • 11
  • 20
  • Right, so thats a way to only update the fields that need to be updated. Thats clear, I have not implemented that as of yet. What about the actual question, logging the changes? Should I make a seperate db table? – cwiggo Oct 23 '14 at 08:26
  • ah sorry. i'd just add add a revision number to the same table, and actually not make a update but add a new row with all the data as well as a higher revision number. – Hampus Brynolf Oct 23 '14 at 08:28
  • revision being the version of updates? say take revision 3, the latest rather than revision 1 the original updates? – cwiggo Oct 23 '14 at 08:33
0

Did you put all that information in a $_SESSION or something? If so, you can unset the session and declare it again, with the new info.

Refilon
  • 3,334
  • 1
  • 27
  • 51
  • yeah, all is in $_FORM[] a copy of $_SESSION – cwiggo Oct 23 '14 at 08:26
  • When the user updates, i assume it's in a form. So what you do, is unset the $_SESSION or $_FORM, and declare it again only with the posted values. – Refilon Oct 23 '14 at 08:27
0

You can use custom setters / getters to do this. Check the code below.

You can also add additional checks to make sure the values have changed, or use magic methods to make things more dynamic.

class MyObject
{
    protected $modifiedFields = array();
    public function setField($value) {
        if (!in_array('field', $this->modifiedFields)) {
            $this->modifiedFields[] = 'field';
        }
    }
}

If you have the modified fields, you can just run the update query to contain only these fields.

Vlad Preda
  • 9,780
  • 7
  • 36
  • 63