简体   繁体   中英

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? Any thoughts? The if statement works fine.

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.

Edit: If your 1A column is not an integer column and the values are strings, you need to quote them.

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

Although that problem would be solved automatically with a prepared statement...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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