dousuo8400 2013-11-26 13:52
浏览 40

仅更新或插入已更改的列

I am experimenting trying to find the best and most efficient way to alter the data in a given table through a form using PHP.

The scenario is a list of items in a table, if you right click->edit an item, a request is made to MySQL for all the data and the fields are populated.

The user can change or leave the data untouched in any of the fields, and then presses save which sends everything back to PHP.

The easy way would be to just update all the columns regardless of whether or not they have changed, i.e.:

$this->model->set('name', 'some name string from the form', $itemId);
$this->model->set('price', 'number from the form', $itemId);
...etc...

So potentially I could change just the name and needlessly update the rest of the columns with the same data as what was received. (As a side question, does MySQL know this and ignore the update behind the scenes?)

Would a good way to perform an intelligent update be to compare two arrays? One that contains the original data and another with the data from the user. If values of a given index don't match, then it must have changed and so do the update?

i.e. a very simplified example:

if($submittedValues['name'] != $originalValues['name'])
{
    ...Update...
}
  • 写回答

1条回答 默认 最新

  • dounielong7728 2013-11-26 13:57
    关注

    I guess you answer your question, and you could compare two array, either in your PHP code or using javascript and instead of sending every thing to the server, only send the changed values.

    But in general I wouldn't care if I reset all the data, the process of affecting all fields again could be faster than comparing between old and new data in arrays, I would take much care if I was making many queries to the database but its only one update query


    What could be interesting in test is, when the user lefts the fields empty, then the request will send an empty string, at the end it the update request will insert an empty string where a NULL value would have a better signification

    评论

报告相同问题?

悬赏问题

  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了