使用循环插入多行

发布于 2024-09-06 18:32:08 字数 385 浏览 8 评论 0原文

代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

污味仙女 2024-09-13 18:32:08

您在 INSERT 语句中缺少字符串连接:

$uz = mysql_query("INSERT INTO `cars`
                     (n_id, auto_id)
                   VALUES "
                    . $insert .") or die(mysql_error());

假设您对元组支持的尾随逗号没有问题。

You're missing string concatentation in the INSERT statement:

$uz = mysql_query("INSERT INTO `cars`
                     (n_id, auto_id)
                   VALUES "
                    . $insert .") or die(mysql_error());

That's assuming you aren't also having an issue with the trailing comma for tuple support.

谁许谁一生繁华 2024-09-13 18:32:08
  • 您的未展开查询是这样的:

    插入 `cars` (n_id, auto_id) 值 '$insert'
    

    然后,随着 $insert 展开,它可能看起来像这样:

    插入 `cars` (n_id, auto_id) 值 '(1,12),(2,12),(3,12)';
    
  • 那些单引号 (') 不应出现在数据列表周围,因为单引号分隔字符串。

  • 相反,您想要:

    插入 `cars` (n_id, auto_id) 值 (1,12),(2,12),(3,12);
    

    将其重新折叠到 PHP 中,最终得到:

    插入 `cars` (n_id, auto_id) 值 $insert
    

tl;dr: 删除 '

  • Your query, unexpanded, is this:

    INSERT INTO `cars` (n_id, auto_id) VALUES '$insert'
    

    Then, with $insert expanded, it probably looks something like this:

    INSERT INTO `cars` (n_id, auto_id) VALUES '(1,12),(2,12),(3,12)';
    
  • Those single quotes (') around the data list should not be there, as single quotes delimit strings.

  • Instead, you want:

    INSERT INTO `cars` (n_id, auto_id) VALUES (1,12),(2,12),(3,12);
    

    Re-collapsing that into your PHP, you end up with:

    INSERT INTO `cars` (n_id, auto_id) VALUES $insert
    

tl;dr: remove the 's.

苯莒 2024-09-13 18:32:08

据我所知,在 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.

梦明 2024-09-13 18:32:08

关于 常规SQL 管理 它给出了插入的示例:

INSERT INTO [table]
( [field1], [field2], [field3] ) VALUES
( '[value1.1]', '[value1.2]', '[value1.3]' ),
( '[value2.1]', '[value2.2]', '[value2.3]' ),
( '[value3.1]', '[value3.2]', '[value3.3]' ),
etc.

您可能会遇到一些语法问题。我建议打印出来

"INSERT INTO `cars`
       (n_id, auto_id)
       VALUES
       '$insert'"

看看您发送的内容。

On General SQL Management it gives this example for an insert into:

INSERT INTO [table]
( [field1], [field2], [field3] ) VALUES
( '[value1.1]', '[value1.2]', '[value1.3]' ),
( '[value2.1]', '[value2.2]', '[value2.3]' ),
( '[value3.1]', '[value3.2]', '[value3.3]' ),
etc.

You might have some syntax problems. I recommend printing out

"INSERT INTO `cars`
       (n_id, auto_id)
       VALUES
       '$insert'"

to see what you are sending.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文