<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
     xmlns:admin="http://webns.net/mvcb/"
     xmlns:content="http://purl.org/rss/1.0/modules/content/"
     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
	<channel> 

	<title>Comments on: Form updates in PHP-MySQL</title>
	<link>http://ask.metafilter.com/62374/Form-updates-in-PHPMySQL/</link>
	<description>Comments on Ask MetaFilter post Form updates in PHP-MySQL</description>
	<pubDate>Thu, 10 May 2007 23:25:02 -0800</pubDate>
	<lastBuildDate>Thu, 10 May 2007 23:25:02 -0800</lastBuildDate>
	<language>en-us</language>
	<docs>http://blogs.law.harvard.edu/tech/rss</docs>
	<ttl>60</ttl>

	<item>
		<title>Question: Form updates in PHP-MySQL</title>
		<link>http://ask.metafilter.com/62374/Form-updates-in-PHPMySQL</link>	
		<description>Rather than updating the entire table every time a user updates a form (their profile page, for instance), I&apos;d like the quickest way to detect ONLY the edited fields, and update those accordingly. 
 &lt;br /&gt;&lt;br /&gt; This will also be useful when updating the system activity log, such as &quot;User John updated e-mail, telephone, password&quot;.&lt;br&gt;
&lt;br&gt;
The conventional way is to embed a hidden &quot;original&quot; field in the form, and then compare both new and original values. I&apos;m looking at a faster solution with less work - seeing that my project has a loooooot of forms &amp;amp; user activities.</description>
		<guid isPermaLink="false">post:ask.metafilter.com,2007:site.62374</guid>
		<pubDate>Thu, 10 May 2007 22:38:24 -0800</pubDate>
		<dc:creator>arrowhead</dc:creator>
		
			<category>php</category>
		
			<category>mysql</category>
		
			<category>form</category>
		
	</item> <item>
		<title>By: orthogonality</title>
		<link>http://ask.metafilter.com/62374/Form-updates-in-PHPMySQL#939227</link>	
		<description>Trigger updating audit table.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2007:site.62374-939227</guid>
		<pubDate>Thu, 10 May 2007 23:25:02 -0800</pubDate>
		<dc:creator>orthogonality</dc:creator>
	</item><item>
		<title>By: rhizome</title>
		<link>http://ask.metafilter.com/62374/Form-updates-in-PHPMySQL#939253</link>	
		<description>Unless this is an academic exercise, updating all fields will likely be faster and simpler than doing an extra query to fill the &quot;original&quot; fields in order to create a second smaller update query. You&apos;d be doing the comparison in the browser as well, which is not the fastest environment and also adds complexity to the solution.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2007:site.62374-939253</guid>
		<pubDate>Fri, 11 May 2007 01:30:42 -0800</pubDate>
		<dc:creator>rhizome</dc:creator>
	</item><item>
		<title>By: devilsbrigade</title>
		<link>http://ask.metafilter.com/62374/Form-updates-in-PHPMySQL#939257</link>	
		<description>Unless you keep info in a table structured like&lt;br&gt;
&lt;br&gt;
INFO_ID | INFO_TYPE | INFO_NAME | INFO_VALUE  | INFO_DATE&lt;br&gt;
------------------------------------------------------------------------------&lt;br&gt;
1             string            email             arrowhead@blah    05-11-2007&lt;br&gt;
&lt;br&gt;
and then key INFO_IDs to USER_IDs, your current solution is the best. There&apos;s some value in keeping history and caching the most recent; in particular, things like generating system activity logs are much smoother. It all depends on what your most common requests will be, and what you need to be fast/easy and what you can live with being inefficient.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2007:site.62374-939257</guid>
		<pubDate>Fri, 11 May 2007 02:09:29 -0800</pubDate>
		<dc:creator>devilsbrigade</dc:creator>
	</item><item>
		<title>By: Aidan Kehoe</title>
		<link>http://ask.metafilter.com/62374/Form-updates-in-PHPMySQL#939267</link>	
		<description>&lt;blockquote&gt;&lt;i&gt;The conventional way is to embed a hidden &quot;original&quot; field in the form, and then compare both new and original values. I&apos;m looking at a faster solution with less work - seeing that my project has a loooooot of forms &amp;amp; user activities.&lt;/i&gt;&lt;/blockquote&gt; With client side JavaScript, on page load store the current values of each form field somewhere; on form submit, compare the current values of those fields with the original values, and disable any form elements  that haven&apos;t changed. Then, on the server, only update those fields that appear in the _POST array.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2007:site.62374-939267</guid>
		<pubDate>Fri, 11 May 2007 03:40:27 -0800</pubDate>
		<dc:creator>Aidan Kehoe</dc:creator>
	</item><item>
		<title>By: and hosted from Uranus</title>
		<link>http://ask.metafilter.com/62374/Form-updates-in-PHPMySQL#939348</link>	
		<description>What Aidan Kehoe said.&lt;br&gt;
&lt;br&gt;
An alternative to disabling input elements onSubmit would be to add each element id to a hidden input list onChange. When the list gets posted, you just update those fields.&lt;br&gt;
&lt;br&gt;
My way has the disadvantage of having to add the onchange attribute to every input element, whereas with Aiden&apos;s approach you could generically handle any form.  But, I dunno,  something about removing expected  post vars altogether seems wonky to me.  I&apos;d be afraid of somehow losing some input I actually needed if something went awry.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2007:site.62374-939348</guid>
		<pubDate>Fri, 11 May 2007 07:03:30 -0800</pubDate>
		<dc:creator>and hosted from Uranus</dc:creator>
	</item><item>
		<title>By: and hosted from Uranus</title>
		<link>http://ask.metafilter.com/62374/Form-updates-in-PHPMySQL#939354</link>	
		<description>Oh, and&lt;br&gt;
&lt;em&gt;on page load store the current values of each form field somewhere;&lt;/em&gt;&lt;br&gt;
&lt;br&gt;
I wouldn&apos;t bother with this.  Instead, on the server side, let php &apos;hardcode&apos; the current values into a javascript array.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2007:site.62374-939354</guid>
		<pubDate>Fri, 11 May 2007 07:06:51 -0800</pubDate>
		<dc:creator>and hosted from Uranus</dc:creator>
	</item><item>
		<title>By: cmicali</title>
		<link>http://ask.metafilter.com/62374/Form-updates-in-PHPMySQL#939366</link>	
		<description>Not sure if this applies to PHP, but Hibernate does something similar to this on the object level.&lt;br&gt;
&lt;br&gt;
Essentially updates look something like this:&lt;br&gt;
1. Get record you are updating from DB and fill into object.&lt;br&gt;
2. Copy form field values into object&lt;br&gt;
3. Persist object&lt;br&gt;
&lt;br&gt;
When you set the field of the object, if they value is the same it won&apos;t make the change, but if they differ it will change and set a dirty flag that says &apos;this field has been changed.&apos;  Then when you get to the persist object step the SQL update is built dynamically to only contain changed fields.  &lt;br&gt;
&lt;br&gt;
This means that for an update you are doing another get, and this overhead is sometimes undesired, but sometimes this is very useful (concurrency/versioning of records, objects with very large numbers of fields, field-level security, etc.)</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2007:site.62374-939366</guid>
		<pubDate>Fri, 11 May 2007 07:21:40 -0800</pubDate>
		<dc:creator>cmicali</dc:creator>
	</item><item>
		<title>By: Deathalicious</title>
		<link>http://ask.metafilter.com/62374/Form-updates-in-PHPMySQL#939466</link>	
		<description>Unobtrusive HTML might work for something like this. You create a javascript include that loops through all inputs and stores them. On submit, it checks the current values for all inputs and removes them if they still match the old values (except I guess for hidden fields, which probably contain the record id).&lt;br&gt;
&lt;br&gt;
Then you&apos;d need to change the code on the back end so it didn&apos;t necessarily expect all fields to be returned by the form.&lt;br&gt;
&lt;br&gt;
But I agree that changing all the fields should not be a big deal. If your tables have a ridiculously huge number of fields, then you&apos;re doing something wrong.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2007:site.62374-939466</guid>
		<pubDate>Fri, 11 May 2007 08:55:24 -0800</pubDate>
		<dc:creator>Deathalicious</dc:creator>
	</item><item>
		<title>By: fishfucker</title>
		<link>http://ask.metafilter.com/62374/Form-updates-in-PHPMySQL#939818</link>	
		<description>as usual, orthogonality has what looks like the &apos;best-practice&apos;, easiest and most elegant solution, as long as your MySQL version supports it (looks like 5.0.2 and above). &lt;a href=&quot;http://dev.mysql.com/doc/refman/5.0/en/using-triggers.html&quot;&gt;See here&lt;/a&gt; for more details. Basically, you should be able to cause your DB to auto-update your log file with only the changed fields on every UPDATE or INSERT statement. You&apos;d only need to build this trigger for each affected table rather than modifying each form.&lt;br&gt;
&lt;br&gt;
i&apos;d recommend against any system that values user-submitted data of the &apos;old&apos; records over what the database says. I can&apos;t think of any scenario where this would cause security problems (unless you were doing other things totally wrong as well), but my belief is that it is good practice to trust the db over the user, as presumedly, it already contains validated data.&lt;br&gt;
&lt;br&gt;
I&apos;m must admit I&apos;m  a little confused by what you mean by &apos;Rather than updating the entire table every time a user updates a form (their profile page, for instance)&apos; -- i suppose if your database is normalized you may get to &apos;skip&apos; a few queries (you might look into using views in this instance), so I suppose I could be missing the point of your question (and thus giving bad advice).  do you have an example you could share?</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2007:site.62374-939818</guid>
		<pubDate>Fri, 11 May 2007 14:11:15 -0800</pubDate>
		<dc:creator>fishfucker</dc:creator>
	</item><item>
		<title>By: fishfucker</title>
		<link>http://ask.metafilter.com/62374/Form-updates-in-PHPMySQL#939820</link>	
		<description>er, sorry -- had a bit of a run-on sentence in my last paragraph there due to haphazard last minute editing.&lt;br&gt;
&lt;br&gt;
my question is -- what are you trying to accomplish by only updating &apos;changed&apos; fields, rather than wholesale sticking all of them in there, changed or not?</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2007:site.62374-939820</guid>
		<pubDate>Fri, 11 May 2007 14:12:53 -0800</pubDate>
		<dc:creator>fishfucker</dc:creator>
	</item><item>
		<title>By: Mick</title>
		<link>http://ask.metafilter.com/62374/Form-updates-in-PHPMySQL#939844</link>	
		<description>why not hidden fields with the original value?&lt;br&gt;
&lt;br&gt;
if foo != foo1 &lt;br&gt;
 update</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2007:site.62374-939844</guid>
		<pubDate>Fri, 11 May 2007 14:58:59 -0800</pubDate>
		<dc:creator>Mick</dc:creator>
	</item><item>
		<title>By: arrowhead</title>
		<link>http://ask.metafilter.com/62374/Form-updates-in-PHPMySQL#940957</link>	
		<description>&lt;i&gt;fishfucker: my question is -- what are you trying to accomplish by only updating &apos;changed&apos; fields, rather than wholesale sticking all of them in there, changed or not?&lt;/i&gt;&lt;br&gt;
&lt;br&gt;
I&apos;m developing an extranet for a company that will be used by both their staff and clients. There&apos;s a quite a few screens where the content can be updated by users, while some are limited to admins.&lt;br&gt;
&lt;br&gt;
The project sponsor wants an &quot;activity log&quot; of all that&apos;s happening in the system - and that includes a record of any database insert or update activities. I&apos;m trying to work on a method that detects only the changed fields, so that the system log will produce something specific like &quot;User John updated e-mail, telephone, password&quot; rather than a general &quot;User John updated his profile&quot;.&lt;br&gt;
&lt;br&gt;
Additionally, I am thinking that with the same method, I can update only those changed fields in the database so that it will be more efficient everytime an update SQL is run.&lt;br&gt;
&lt;br&gt;
Many thanks in advance for all replies - I&apos;m reading up on the &quot;trigger&quot; suggestion - any other ideas will be much appreciated.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2007:site.62374-940957</guid>
		<pubDate>Sun, 13 May 2007 08:18:58 -0800</pubDate>
		<dc:creator>arrowhead</dc:creator>
	</item><item>
		<title>By: fishfucker</title>
		<link>http://ask.metafilter.com/62374/Form-updates-in-PHPMySQL#940998</link>	
		<description>yeah, it looks like a trigger is really going to suit you well for the activity log. &lt;br&gt;
&lt;br&gt;
as far as &apos;saving&apos; work by only updating specific fields, my guess is that the calculation of finding those fields will be greater than the time it would take to just update all of them, particularly if there are few tables involved. I wouldn&apos;t worry too much about it.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2007:site.62374-940998</guid>
		<pubDate>Sun, 13 May 2007 09:03:40 -0800</pubDate>
		<dc:creator>fishfucker</dc:creator>
	</item>
	</channel>
</rss>
