用于更新MYSQL Row的PHP表单

时间:2016-08-03 20:43:01

标签: php mysql

我有一个更新MySQL数据库的PHP表单。我想在行中添加当前信用额度。基础数学

当前积分50再增加50个积分= 100

这是我当前的字符串(EDITED)

    $sql = sprintf("UPDATE h_clients ". "SET credits = $credits + '%s'" . "WHERE id = $id",

Currently this works but it doesn't add the new credits with the current credits in DB 

Here is the full code:

$sql = sprintf("UPDATE h_clients ". "SET credits = $credits + '%s'" . "WHERE id = $id",

 mysql_real_escape_string($credit),
               mysql_real_escape_string($id));

            if (mysql_query($sql, $con)) {
                $insertSuccessful = true;
            } else {
                echo $sql;
                echo "\n" . mysql_error($con);
                echo "mysql err no : " . mysql_errno($con);
            }
    }

    }

    return $insertSuccessful;

2 个答案:

答案 0 :(得分:1)

如果您只是添加存储在数据库中的值,您应该能够这样做:

$sql = sprintf("UPDATE h_clients SET credits = credits + $credits WHERE id = $id",

 mysql_real_escape_string($credit),
            mysql_real_escape_string($id));

请说明这是不是您的意图。

答案 1 :(得分:0)

似乎您在分配信用中有错误的列名

"UPDATE h_clients SET credits = credits +  $credits  WHERE id = $id",