PHP 表单更新 MYSQL 行

标签 php mysql

<分区>

我有一个更新 MySQL 数据库的 PHP 表单。我想将学分添加到行中的当前学分。基础数学

当前积分 50 再加上 50 积分 = 100

这是我当前的字符串(已编辑)

    $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;

最佳答案

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

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

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

请澄清这是否不是您想要的。

关于PHP 表单更新 MYSQL 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38753257/

相关文章:

php - 如何使用终端/PHP 测试 PHP 套接字 TCP/IP 服务器?

php - Zend 框架 : Error Reporting is shut down regardless of ini File

MySQL 组按 : Only include rows with specific count of column

php - MySQL/PHP preg_replace 和 KEEP 重音符号和空格

php - 每个查询中的条件,CakePHP 2.5.4

PHP - 列出用户发布的每个项目

php - mysql忽略where语句

php - 字段值上的 MySQL 通配符?

javascript - 在 JQuery 中插入 PHP

mysql - SQL:查找每个运行者运行之间的平均天数