[英]Having trouble with SQL query
I'm using this query (I changed it): 我正在使用此查询(我更改了它):
// SQL query
$squl = "INSERT INTO 'messages' ('id','name' ,'email' ,'subject' ,'content','userid') VALUES ( null,'".$name."', '".$mail."', '".$subject."', '".$content."','');";
// mysql query
$query = mysql_query($squl) or die("message query problem: ". mysql_error());
I get this error: 我收到此错误:
You have an error in your SQL syntax;
您的SQL语法有误; check the manual that corresponds to your MySQL server version for the right syntax to use near ''messages' ('id','name' ,'email' ,'subject' ,'content','userid' )VALUES ( null,'' at line 1
检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在``messages''('id','name','email','subject','content','userid')附近使用VALUES(null, ''在第1行
What is causing this? 是什么原因造成的?
.``)
You used a period here instead of a comma so the function is only receiving 5 columns when it needs 6. .``)
您在此处使用句点而不是逗号,因此该功能在需要6列时仅接收5列。
Update: 更新:
As the commenter below points out, you've replaced the backticks with quotation marks. 正如下面的评论者所指出的,您已经用引号将反引号替换了。
$squl="INSERT INTO `messages` (`id`,`name` ,`email` ,`subject` ,`content`,`userid` )VALUES ( null,'$name', '$mail', '$subject', '$content','');";
(id,name ,email ,subject ,content,userid )
( NULL,".$name.", ".$mail.", ".$subject.", ".$content."**.**``);
you are using '.' 您正在使用 '。' instead of ,
代替 ,
Well, that's about the clearest message you get from SQL. 好吧,这是您从SQL获得的最清晰的消息。 You try to insert 5 values into 6 columns.
您尝试将5个值插入6列。
The problem that there's no comma between the last two values. 最后两个值之间没有逗号的问题。 Instead there's a
.
而是有一个
.
which makes the parser think it's only one value. 这使解析器认为这只是一个值。
You are trying to insert into 6 columns: 您尝试插入6列:
id
name
email
subject
content
userid
But have only specified 5 values: 但是只指定了5个值:
NULL
$name
$mail
$subject
$content
You've got a dot where you should have a comma: 您应该在逗号处加点:
".$subject."`, `".$content."`.``);";
Change that last dot to a comma and you should be golden 将最后一个点更改为逗号,您应该会很高兴
You've got 6 fields in your fields list, but are inserting only 5 values in your values list. 您的字段列表中有6个字段,但是在值列表中仅插入5个值。 Looks like you've got a
.
看来您有一个
.
instead of a ,
: 而不是
,
:
`, `".$subject."`, `".$content."`.``
^--- here
As well, there is NO reason to use repeated string concatenation as you are. 同样,没有理由照原样使用重复的字符串连接。 PHP can insert variables into double-quoted strings quiet easily:
PHP可以轻松地将变量插入双引号的字符串中:
$sql = "INSERT INTO .... (...) VALUES (NULL, '$name', '$mail', '$subject', '$content', '')";
Note that the 'null' value is not quoted. 请注意,“ null”值未加引号。 Backticks are there to escape reserved words.
反引号可用来逃避保留字。 If you intend to insert a real database null, then use the bare word
null
. 如果打算插入一个真实的数据库null,则使用裸词
null
。 If you want a literal string 'null' to go in, then quote it normally: 'null'
. 如果您想输入文字字符串“ null”,则通常使用引号:
'null'
。
You have six fields listed the first set of parentheses and only five fields in VALUES. 您有六个字段列出了第一组括号,而在VALUES中只有五个字段。 That's what column count means.
这就是列数的含义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.