2013-04-22 23 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)在所有放緩。

相關問題