【问题标题】:How to add/substract data from the database [closed]如何从数据库中添加/减去数据[关闭]
【发布时间】:2015-01-07 23:12:29
【问题描述】:

我必须从数据库中添加一些数据我正在使用此代码从数据库中获取它

$query = "SELECT * FROM `payments` ";
$query .= "WHERE id = '{$_SESSION['user_id']}' ";
$query .= "AND complete = 1 ";
$query .= "AND type = 'script'";
$scripts = $database->query($query);

我正在使用这个 foreach 循环来尝试将金额相加:

foreach($scripts as $s) {
    $spendscripts + $s['amount'];
}

这就是数据库的样子:

但是,当我回显 $spendscripts 时,它只是空的,而当我分配默认值时,它只是那个默认值

提前致谢

【问题讨论】:

  • 为什么不直接使用SELECT SUM(amount) AS total_amount FROM ...
  • 不应该是WHERE user_id =,而不是WHERE id =吗?
  • 您应该打印 SQL 并尝试在客户端执行以查看发生了什么问题
  • 一旦将它们添加在一起,您就不会将数据保存在任何地方。
  • @Barmar 谢谢!我没有看到,但是有了这个,我无法通过 id 和所有其他人订购它,但它现在已修复,谢谢:D

标签: php mysqli foreach add


【解决方案1】:


$spendscripts + $s['amount'];
更改为
$spendscripts = $spendscripts + $s['amount'];
并将$spendscripts 的默认值设为零

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-30
    • 2019-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-01
    相关资源
    最近更新 更多