这是我的代码:
$gid = (int) stripslashes($_POST['id']);
echo $gid;
$db = dbConnect();
$test = $db->query('update games set played = played + 1 where id = "$gid"');
echo $db->error;
echo $db->errno;
die();
}
它从终端工作正常,它正确打印出$ gid,并且不返回任何错误。我错过了一些非常明显的东西吗?
答案 0 :(得分:3)
您将查询括在单引号中。在单引号中,变量插值(也称为替换)不会发生。
简单示例:
$who = 'harry potter';
echo 'hi "$who"'; // prints hi "$who"
echo "hi '$who'"; // prints hi 'harry potter'
将您的代码更改为:
$test = $db->query("update games set played = played + 1 where id = '$gid'");
同样来自行$gid = (int) stripslashes($_POST['id']);
,明确$gid
是一个整数,并且不需要在查询中将其括在引号中。所以我们有:
$test = $db->query("update games set played = played + 1 where id = $gid");
答案 1 :(得分:1)
$test = $db->query("UPDATE `games` SET `played` = `played` + 1 WHERE `id` = '" . $gid . "'");
答案 2 :(得分:0)
单引号中的任何内容都不会被解析。做id="' . $gid . '"
或者将整个事物用双引号括起来并在$ gid周围加上单引号。
答案 3 :(得分:0)
尝试
$test = $db->query
("
UPDATE `games`
SET
`played` = (`played` + 1)
WHERE `id` ='" . $gid . "'
");