每当调用脚本时,我都试图在数据库中更新int
值。为了解决这个问题,我有一个名为monthly_flagged
的表,它可以获取我网站上所有标记的帖子,并将它们分类到标记的月份。该脚本名为flagged_posts
。调用脚本时,我们的想法是表total_flagged
中的列monthly_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'");
}
答案 0 :(得分:0)
在您的更新声明中,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
P.S。您的代码容易受到SQL注入的攻击,这意味着其他人可以在您的数据库中执行任何SQL代码,包括删除所有SQL代码。你应该阅读准备好的陈述以及如何使用它们! More info on prepared statements