简体   繁体   English

为什么这次更新不更新分数?

[英]Why doesnt this update update the score?

I am trying to create an update that whenever the user gets an offer lower than the last day/week average price they get points for it 50 for last day and 20 for last week我正在尝试创建一个更新,每当用户获得低于最后一天/每周平均价格的报价时,他们会为此获得积分,最后一天为 50,上周为 20

I am desperate I have been trying for the longest time to chage thing on the proyect and now when I try to update that for every time the condition is met to update the db, it does its work and see when the price is cheaper and where it isnt but never increases the score which bugs me a lot¿can someone help?我很绝望,我已经尝试了最长的时间来更改项目上的东西,现在当我尝试在每次满足更新数据库的条件时更新它时,它会完成它的工作并查看价格何时更便宜以及在哪里它不会但永远不会增加让我很烦恼的分数,有人可以帮忙吗?

function check_if_offer_cheaper_with_twenty_than_previous_day($prodname, $price)
// Check if the price found by the user is 20% lower than the most recent available average price of the previous day
{
    global $db;
    $sql = 'SELECT AVG(prices.price) >"' . $price /  0.8 . '" AS cheaper_than_previous_day'
        . " FROM prices"
        . ' WHERE prices.prodname = "' . $prodname . '"' .
        ' AND prices.date = CURDATE() - INTERVAL 1 DAY';


    $result = mysqli_query($db, $sql);
    $row = mysqli_fetch_assoc($result);

    if (isset($row['cheaper_than_previous_day'])) {
        $query = "UPDATE users SET total_score = total_score +50,
        score_this_month = score_this_month + 50 WHERE id = '" . $_SESSION['user']['id'] . "'";
        mysqli_query($db, $query);
        return $row['cheaper_than_previous_day'];
    } else {
        return true;
    }
}

function check_if_offer_cheaper_with_twenty_than_previous_week($prodname, $price)
// Check if the price found by the user is 20% lower than the most recent average price available in the previous week
{
    global $db;
    $sql = 'SELECT AVG(prices.price) > "' . $price / 0.8 . '" AS cheaper_than_previous_week'
        . " FROM prices"
        . ' WHERE prices.prodname = "' . $prodname .
        '" AND prices.date BETWEEN CURDATE() - INTERVAL 8 DAY AND CURDATE() - INTERVAL 1 DAY';

    $result = mysqli_query($db, $sql);
    $row = mysqli_fetch_assoc($result);

    if (isset($row['cheaper_than_previous_week'])) {
       $query = "UPDATE users SET total_score = total_score +20, score_this_month = score_this_month +50           
       WHERE id = '" . $_SESSION['user']['id'] . "'";
        mysqli_query($db, $query);
        return $row['cheaper_than_previous_week'];
    } else {
        
        return true;
    }
}

MySQL syntax uses commas to separate the assignment pairs, like this MySQL 语法使用逗号分隔赋值对,如下所示

       $query = "UPDATE users SET total_score = total_score +20, 
                 score_this_month = score_this_month +50
                 WHERE id = '" . $_SESSION['user']['id'] . "'";           

dev.mysql 8.0 update syntax dev.mysql 8.0更新语法

That is assuming that $_SESSION['user']['id'] does not evaluate to null.这是假设$_SESSION['user']['id']计算结果不是 null。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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