简体   繁体   English

MySQLi更新的增量值

[英]Increment value on MySQLi Update

Simply trying to add points to an existing value in a column named 'Points'. 只需尝试将点添加到名为“点”的列中的现有值。 I've read a few articles that suggest the beneath, but it's not working for me. 我已经阅读了一些暗示下面的文章,但这对我不起作用。 Perhaps because I'm using mysqli rather than mysql? 也许是因为我使用的是mysqli而不是mysql? Any thoughts? 有什么想法吗? The if statement works fine. if语句工作正常。

if (!empty($m1A) && ($r1A == 0)) {
    // Rewards pts
    $query = "UPDATE users SET Points=Points+3 WHERE 1A='$m1A'";
    $result = mysqli_query($conn, $query);

    // Record reward of pts
    $query1 = "UPDATE rounds SET 1A = 1";
    $result2 = mysqli_query($conn, $query1);
}

If your column name starts with a number, you have to quote it in backticks: 如果列名以数字开头,则必须在反引号中引用它:

$query = "UPDATE users SET Points=Points+3 WHERE `1A`=$m1A";

and: 和:

$query1 = "UPDATE rounds SET `1A` = 1";

And I would recommend using a prepared statement with bound parameters to avoid sql injection problems. 我建议使用带有绑定参数的预准备语句来避免sql注入问题。

Edit: If your 1A column is not an integer column and the values are strings, you need to quote them. 编辑:如果您的1A列不是整数列且值是字符串,则需要引用它们。

$query = "UPDATE users SET Points=Points+3 WHERE `1A`='$m1A'";
                                                      ^    ^

Although that problem would be solved automatically with a prepared statement... 虽然这个问题可以通过准备好的声明自动解决......

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

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