gpt4 book ai didi

php - 调用脚本时数据库中的增量值

转载 作者:行者123 更新时间:2023-11-30 22:21:05 26 4
gpt4 key购买 nike

每当调用脚本时,我都试图在我的数据库中获取所有更新 int 值。为了正确看待这个问题,我有一个名为 monthly_flagged 的表,它获取我网站上所有被标记的帖子,并将它们分类到被标记的月份。该脚本称为 flagged_posts。调用脚本时,想法是表 monthly_flagged 中的列 total_flagged 将增加一 (+1)。

但是,根据我目前的方法,数字不会改变。当前值为 0,我标记了 3 个帖子,这意味着 total_flagged 应该等于 3,但它仍然为 0(没有变化)。

这是我的方法:

$currentMonth = date('F'); // get month realtime
$currentYear = date ('Y'); // get year realtime

$check_month = mysqli_query ($connect, "SELECT * FROM monthly_flagged");
$get_month = mysqli_fetch_array ($check_month);
$month = $get_month['month'];
$year = $get_month['year'];
$total_flagged = $get_month['total_flagged'];

// check if row exists in db for current month/year, else make it.
$checking_row = mysqli_query($connect, "SELECT * FROM monthly_flagged WHERE month = '$currentMonth' AND year = '$currentYear' LIMIT 1");
$check_return = mysqli_num_rows ($checking_row);

if ($check_return == 0){
$make_row = mysqli_query ($connect, "INSERT INTO monthly_flagged VALUES ('', '$currentMonth', '$currentYear', '')");
}
if ($check_return == 1){
$plusOne = $total_flagged +1;
$update_total_flagged = mysqli_query ($connect, "UPDATE monthly_flagged WHERE month = '$currentMonth' AND year = '$currentYear'
SET total_flagged = '$plusOne'");
}

最佳答案

在您的更新语句中,您将 WHERE 放在 SET 前面,它应该是:

UPDATE monthly_flagged 
SET total_flagged = '$plusOne'
WHERE month = '$currentMonth' AND year = '$currentYear'

或者没有加一变量:

UPDATE monthly_flagged 
SET total_flagged = (total_flagged+1)
WHERE month = '$currentMonth' AND year = '$currentYear'

如果您实现一些东西来捕获 MySQL 错误(它们不会神奇地显示出来),那么这些类型的错误很容易被发现:More info on error reporting

附言您的代码容易受到 SQL 注入(inject)的攻击,这意味着其他人几乎可以在您的数据库上执行任何 SQL 代码,包括全部删除。您应该阅读准备好的陈述以及如何使用它们! More info on prepared statements

关于php - 调用脚本时数据库中的增量值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36609279/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com