2013-04-22 46 views
0

我正在使用Codeigniter构建网站。该网站有一个名为“更新记录”的选项,允许用户更新50个不同的值。更新页面具有旧值以及用于输入新值的空文本框。即使用户没有真正更新任何内容,也更新整个记录是没有意义的(网站使用MySQL数据库)。我试着用原始值使用隐藏的表单域,并试图只更新修改过的域。虽然它工作正常,但每次将旧值与新值进行比较时,该过程看起来非常冗长,只有在发生更改时才会更新。如何仅更新php中的修改后的值

有没有更好的方法来做到这一点(有或没有使用codeigniter)?只是好奇知道。 在此先感谢。

+0

改变方法。把旧的价值放在文本框中,让用户在那里改变。如果它相同(即用户没有触及它),MySQL会注意到这一点并且不会更新它。 – itachi 2013-04-22 07:33:46

+0

@itachi但我维护所有更新的日志表。我如何仅使用修改后的值更新日志表? – ben 2013-04-22 07:48:20

+0

哈哈,你应该早点提到它。 – itachi 2013-04-22 07:57:34

回答

0

我会解析post数组,并删除任何没有更新值的条目(这意味着输入被提交为空)。

然后,你可以只更新修改的记录

我不知道代码点火器表单提交后,如何通过接线柱阵列到控制器,但你可以做类似

<?php 
foreach ($postData as $key => $value) { 
    if (empty($value)) { 
     unset($postData[$key]); 
    } 
} 

唯一的地方如果您合法地想要将空记录存储为已更新的值,则这将会下降。

0

我使用JavaScript来做到过滤器只更新值

1 - I use hashes.js to calculate a sha1 when page loaded. store it in hidden or just a variable, or you can do sha in the server. 
2 - I also do a snapshot of the form in array when page loaded. I put all my input in one class and use jQuery('forminputs').each to put them in array. 
3 - when user click submit, first I do another snapshot of the form as No. 2 and compare the hash. if the hash diff, I use php.js get the updated value php.array_diff_assoc(newsnapshot, oldsnapshot). and post this to server. 

虽然这些看起来很多计算的,但实际上它不是在Firefox或Chrome(永远不要试图IE)在所有放缓。

相关问题