Home > PHP Framework > ThinkPHP > body text

How to modify only one value in thinkphp

藏色散人
Release: 2022-12-05 10:37:42
Original
1264 people have browsed it

Thinkphp method of modifying only one value: 1. Modify the data by calling the save() function under Model; 2. Through "$res = $user->where('ID=5') ->save($data);" method to add where condition, and then call the save function to modify the data value.

How to modify only one value in thinkphp

The operating environment of this tutorial: Windows 7 system, ThinkPHP version 5, Dell G3 computer.

How to modify only one value in thinkphp?

Database operation: modify a piece of data (thinkPHP)

The first method: the database contains an automatically growing field ID, and the ID is used as the where condition

<?php
class UserAction extends Action{
 public function updateUser(){
  $user = M(&#39;Userinfo&#39;);      //实例化Model
  $data = array(&#39;ID&#39;=>5,&#39;username&#39;=>&#39;王美人&#39;,&#39;email&#39;=>&#39;meiren@163.com&#39;);//$data中包含有自动增长字段uid
  $res = $user->save($data);//调用Model下的save()函数进行数据的修改
  var_dump($res);
 }
}
?>
Copy after login

Second method: If there is no automatic growth of field ID in the database, you need to add where conditions

<?php
class UserAction extends Action{
 public function updateUser(){
  $user = M(&#39;userinfo&#39;);//实例化Model
  $data = array(&#39;username&#39;=>&#39;王美人&#39;,&#39;email&#39;=>&#39;mei@163.com&#39;);//$data中没有自动增长字段uid
  $res = $user->where(&#39;ID=5&#39;)->save($data);//需要增加where条件。调用Model下的save()函数进行数据的修改
  var_dump($res);//返回影响的行数
 }
}
?>
Copy after login

Recommended learning: "thinkPHP Video Tutorial"

The above is the detailed content of How to modify only one value in thinkphp. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!