Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
775 views
in Technique[技术] by (71.8m points)

php - 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);
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...