在PHP中使用UPDATE在数据库中增加我的变量 [英] Increment my variable in a database with UPDATE in PHP

查看:62
本文介绍了在PHP中使用UPDATE在数据库中增加我的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从数据库中获取了变量"stock".我想使用以下代码进行更新来增加变量:

I get the variable 'stock' from my database. I would like increment my variable with an update with this code:

$stock = $_POST['stock']

$nom = 'salade';
$req=("UPDATE ingredients SET stock=$stock+1 WHERE nom = :nom;");
 $stmt = $db->prepare($req);

    // execute the query
    $stmt->execute(array('nom'=>$nom));

但是问题是我的变量增加了8,而不是1,因为我有8个元素(沙拉,西红柿,面包等).我想用UPDATE递增1.

But the problem is that my variable increment by 8 and not by 1 because I have 8 element (salade,tomatoes, bread etc). I want to increment by 1 with UPDATE.

感谢阅读我!

推荐答案

考虑到您说的是,您需要做的是每行增加1 WHERE nom =:nom (我想您实际代码不是用$而不是错误的:,或者说您已经实现了它甚至不会增加8" ...记住,请对您的问题进行编辑):

Considering that you're saying all you need to is increment by 1 each row WHERE nom = :nom (I guess your actual code isn't wrong with $ instead of : or it wouldn't even "increment by 8" as you said you're already achieving... remember do edit this on your question, please):

UPDATE
    ingredients
SET
    stock=stock+1
WHERE
    nom = :nom;

Jusk选择您已有的东西,然后添加1个 stock = stock + 1 .

Jusk pick what you already had and add 1 stock=stock+1.

这篇关于在PHP中使用UPDATE在数据库中增加我的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆