php 如何添加到 MySQL DB 中的当前值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4632866/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 13:46:26  来源:igfitidea点击:

How to Add to Current Value in MySQL DB

phpmysql

提问by Robert M Nelson

Hi i have a DB Entry that only consists of a number. and i want to Make it where users on my site can Exchange from One to the Other and i know how to do all that but i dont wanna Replace the current value i just wanna add for example 1,000 to it what code can i use in PHP ?

嗨,我有一个只包含一个数字的数据库条目。我想让我的网站上的用户可以从一个人交换到另一个人,我知道如何做这一切,但我不想替换当前值我只想添加例如 1,000 我可以在 PHP 中使用什么代码?

回答by Mohammad Efazati

<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("my_db", $con);

mysql_query("UPDATE `some_table` SET `value` = `value` + 1000 WHERE `id` = 1");

mysql_close($con);
?> 

update like this

像这样更新

回答by Alex

if you have a value in some table in some database all you have to do is to issue an update statement for that fields.

如果您在某个数据库的某个表中有一个值,您所要做的就是为该字段发出更新语句。

For example, lets say we have table like this

例如,假设我们有这样的表

+--------------+
| some_table   |
+--------------+
| id   | value |
+--------------+
| 1    | 10    |
+--------------+

So your update will be as follows:

所以你的更新如下:

UPDATE `some_table` SET `value` = `value` + 1000 WHERE `id` = 1

See more info on how to execute mysql queries with php.

查看有关如何使用 php 执行 mysql 查询的更多信息。