2 回答
TA贡献1780条经验 获得超1个赞
SELECT {$rqid}, {$sid}, {$rqvcom}, NOW()
这将选择这些变量中的列名:
// let's say these are the values in your variables:
$rqid = "text";
$sid = "session123";
$rqvcom = "example";
// then this query:
SELECT {$rqid}, {$sid}, {$rqvcom}, NOW()
// is the same as:
SELECT text, session123, example, NOW()
这就是您收到该错误的原因。这极不可能是您想要的。我怀疑您真正想要的是选择列rqid,sid并且rqvcom:
SELECT rqid, sid, rqvcom, NOW()
编辑:现在您已经添加了表结构,您似乎更有可能想要实际选择字符串值,在这种情况下,您需要将变量用引号括起来,如@Giacomo M 建议:
SELECT '{$rqid}', '{$sid}', '{$rqvcom}', NOW()
// which will become:
SELECT 'text', 'session123', 'example', NOW()
那么您的数据库将不会将字符串与列名混淆。
TA贡献1827条经验 获得超7个赞
如果您尝试将 的值$rqvcom插入数据库,则需要将其放在引号中,因为它是一个字符串。否则,它被视为列名。
$conn->query("
INSERT INTO reqviews (rqvrqid, rqvuserid, rqvcom, rqvdate)
SELECT {$rqid}, {$sid}, '{$rqvcom}', NOW()
FROM requests
WHERE EXISTS (
SELECT rqid
FROM requests
WHERE rqid = {$rqid})
AND NOT EXISTS (
SELECT rqvid
FROM reqviews
WHERE rqvuserid = {$sid}
AND rqvrqid = {$rqid})
LIMIT 1
");
但是,这会让您容易受到 SQL 注入的影响,因此您应该使用准备好的语句。
$stmt = $conn->prepare("
INSERT INTO reqviews (rqvrqid, rqvuserid, rqvcom, rqvdate)
SELECT ?, ?, ?, NOW()
FROM requests
WHERE EXISTS (
SELECT rqid
FROM requests
WHERE rqid = ?)
AND NOT EXISTS (
SELECT rqvid
FROM reqviews
WHERE rqvuserid = ?
AND rqvrqid = ?)
LIMIT 1
");
$stmt->bind_param("iisiii", $rqid, $sid, $rqvcom, $rqid, $sid, $rqid);
$stmt->execute();
- 2 回答
- 0 关注
- 325 浏览
添加回答
举报