[英]Getting error "Unknown column 'text' in 'field list'"
I am trying to insert text to my database table reqviews
.我正在尝试将文本插入到我的数据库表
reqviews
。 But I get the error message:但我收到错误消息:
Unknown column 'text' in 'field list'
. Unknown column 'text' in 'field list'
。 I'm not sure what's wrong with my code.我不确定我的代码有什么问题。 Please help
请帮忙
if (isset($_POST['combut'])) {
$rqvcom = $_POST['rqvcom'];
$rqid = (int)$_GET['rqid'];
$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
");
}
What my code does: When some text is typed into rqvcom
textarea, that text gets iserted to rqvcom
column on reqviews
table.我的代码做什么:当一些文字键入到
rqvcom
文本区域,该文本被iserted到rqvcom
在列reqviews
表。 If a row already exists on reqviews
table with a rqvuserid
user, then another row with that same userid cannot be inserted.如果在
reqviews
表中已经存在具有rqvuserid
用户的行,则不能插入具有相同用户 ID 的另一行。 My problem is my code gives me the error message I provided.我的问题是我的代码给了我提供的错误信息。
Edit: Table reqviews
编辑:表
reqviews
rqvid | rqvrqid | rqvuserid | rqvcom | rqvdate
Table requests
表
requests
rqid
SELECT {$rqid}, {$sid}, {$rqvcom}, NOW()
This will select the column names that are in those variables:这将选择这些变量中的列名:
// 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()
That is why you're getting that error.这就是您收到该错误的原因。 It is highly unlikely that this is what you want.
这极不可能是您想要的。 I suspect that what you actually want is to select the columns
rqid
, sid
and rqvcom
:我怀疑您真正想要的是选择列
rqid
、 sid
和rqvcom
:
SELECT rqid, sid, rqvcom, NOW()
Edit : Now that you've added your table structures, it seems more likely that you want to actually select the string values, in which case you need to wrap the variables in quotes like @Giacomo M suggested:编辑:现在您已经添加了您的表结构,您似乎更有可能想要实际选择字符串值,在这种情况下,您需要将变量用引号括起来,如@Giacomo M 建议:
SELECT '{$rqid}', '{$sid}', '{$rqvcom}', NOW()
// which will become:
SELECT 'text', 'session123', 'example', NOW()
Then your database will not confuse the strings with column names.那么您的数据库不会将字符串与列名混淆。
If you're trying to insert the value of $rqvcom
into the database, you need to put it in quotes, since it's a string.如果您尝试将
$rqvcom
的值插入数据库,则需要将其放在引号中,因为它是一个字符串。 Otherwise, it's treated as a column name.否则,它被视为列名。
$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
");
However, this leaves you open to SQL injection, so you should use a prepared statement.但是,这会让您容易受到 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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.