使用循环插入多行
代码:
for($i = 1; $i <= $arr['var']; $i++) {
if($i == $arr['var']):
$insert .= '('.$n_id.', 12)';
else:
$insert .= '('.$n_id.', 12),';
endif;
}
$uz = mysql_query("INSERT INTO `cars`
(n_id, auto_id)
VALUES
'$insert'") or die(mysql_error());
为什么它给我一个 SQL 语法错误?我做错了什么?
Code:
for($i = 1; $i <= $arr['var']; $i++) {
if($i == $arr['var']):
$insert .= '('.$n_id.', 12)';
else:
$insert .= '('.$n_id.', 12),';
endif;
}
$uz = mysql_query("INSERT INTO `cars`
(n_id, auto_id)
VALUES
'$insert'") or die(mysql_error());
Why it gives me a SQL syntax error? What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您在 INSERT 语句中缺少字符串连接:
假设您对元组支持的尾随逗号没有问题。
You're missing string concatentation in the INSERT statement:
That's assuming you aren't also having an issue with the trailing comma for tuple support.
您的未展开查询是这样的:
然后,随着
$insert
展开,它可能看起来像这样:那些单引号 (
') 不应出现在数据列表周围,因为单引号分隔字符串。
相反,您想要:
将其重新折叠到 PHP 中,最终得到:
tl;dr: 删除
'
。Your query, unexpanded, is this:
Then, with
$insert
expanded, it probably looks something like this:Those single quotes (
'
) around the data list should not be there, as single quotes delimit strings.Instead, you want:
Re-collapsing that into your PHP, you end up with:
tl;dr: remove the
'
s.据我所知,在 mySQL 中,当与单引号(在 $insert 周围)混合使用时,使用反引号(在汽车周围)是不正确的。选择其中之一 - 可能建议使用单引号进行标准化。
Using backticks (around cars) is incorrect when mixed with single quotes (around $insert), to the best of my knowledge, in mySQL. Pick one or the other - single quotes are probably recommended for standardization.
关于 常规SQL 管理 它给出了插入的示例:
您可能会遇到一些语法问题。我建议打印出来
看看您发送的内容。
On General SQL Management it gives this example for an insert into:
You might have some syntax problems. I recommend printing out
to see what you are sending.