Home > PHP Framework > ThinkPHP > How to solve thinkphp save failure problem

How to solve thinkphp save failure problem

藏色散人
Release: 2021-12-22 11:08:57
Original
3289 people have browsed it

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.

How to solve thinkphp save failure problem

#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();
Copy after login

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。
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template