简体   繁体   English

使用PHP在表单提交中将数据库表值增加一

[英]Using PHP to increase database table value by one on form submit

I am trying to increase the integer value of a database field value each time a form is submitted. 我试图每次提交表单时增加数据库字段值的整数值。

My table is simple. 我的桌子很简单。 Only two relevant fields are Name and xp, and relevant values are Brian and 1. 只有两个相关字段是Name和xp,相关值是Brian和1。

I've gotten to the point I can call the PHP function on form submit, but I can't quite figure out how to declare the variable for the specific field I'm looking to increase ("1") or how to plug it into the UPDATE query. 我已经到了可以在表单提交上调用PHP函数的地步,但是我还不太清楚如何为我要增加的特定字段声明变量(“ 1”)或如何将其插入进入UPDATE查询。

If want to just update it with an arbitrary number, I know I can use... 如果只想用任意数字更新它,我知道我可以使用...

if (isset($_POST['update'])){
$UpdateQuery = "UPDATE points SET xp=9 WHERE Name='Brian'";
mysql_query($UpdateQuery, $conn);

But how do I declare the existing ("1") from the database as a variable, and then plug it into the UPDATE query so that it's something like ....."UPDATE points SET xp=($result + 1) Where Name ='Brian'"; 但是,如何将数据库中的现有变量(“ 1”)声明为变量,然后将其插入到UPDATE查询中,使其类似于.....“ UPDATE points SET xp =($ result + 1)where名字='Brian'“; and where $result is the variable for the desired database value, in this case ("1"). $ result是所需数据库值的变量,在这种情况下为“ 1”。

Desired result is that the database value for xp where Name = Brian is now ("2"). 所需结果是xp的数据库值(其中Name = Brian)现在为(“ 2”)。

I tried using the fetch/select functions to declare the database value as a variable but could not quite wrap my head around it. 我尝试使用fetch / select函数将数据库值声明为变量,但无法完全理解。 Guess I'm kind of new at this. 猜猜我对此很陌生。 Help would be really appreciated. 帮助将不胜感激。

假设xpxp为数字类型,则可以使用:

$UpdateQuery = "UPDATE points SET xp=xp + 1 WHERE Name='Brian'";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM