Thinkphp save failed solution: 1. Open the corresponding code file; 2. Use "$this->typeModel->field('id,name,sort')->data($ data)->save();” method to update the data.
#The operating environment of this article: Windows 7 system, thinkphp v5.1 version, Dell G3 computer.
How to solve the thinkphp save failure problem?
thinkphp save() update failed
1. Failure case
$data = I(); $rs = $this->typeModel->data($data)->save();
2. Correct case
正确一 $rs=$this->typeModel->field('id,name,sort')->data($data)->save(); 正确二: // $rs=$this->typeModel->where('id='.$data['id'])->field('name,sort')->data($data)->save(); 正确三: // $rs=M('CityCategory')->where('id='.$data['id'])->field('name,sort')->data($data)->save(); //注意: //1.如果新数据与原数据相同则跟新失败 //2.不加field 限制会导致修改失败 //3.需要在field中写主键id和要改的字段, 或where条件中写主键id。
3. Cause analysis
All the fields id pid name path sort in the value $data received from the front desk have values, but some new data are the same as the old data.
When there are certain fields in the new data and the old data that are the same, you need to use field to determine the data that needs to be changed. It is estimated that it is written in the field, even if it is the same, it will be updated.
Recommended learning: "The latest 10 thinkphp video tutorials"
The above is the detailed content of How to solve thinkphp save failure problem. For more information, please follow other related articles on the PHP Chinese website!