我试图通过使用implode函数创建INSERT STATEMENT,从数组中插入多行。我无法弄清楚为什么我得到2条以上的INSERT STATEMENT行,因为只有2行。
我也查看了此链接,但insert multiple rows via a php array into mysql仍然没有乐趣
请帮助!
下面是我的代码:
while ($row = sqlsrv_fetch_array($getResults,SQLSRV_FETCH_ASSOC))
{
foreach ($row as $v) {
$sql[] = "(".$row['ReportName'].");";
}
$test = "INSERT INTO PublishedComments (ReportName) VALUES ".implode(",",$sql);
echo $test;
echo "<br />...........<br />";
}
输出如下:
INSERT INTO PublishedComments (ReportName) VALUES (Report Sales Per Order Method Type);,(Report Sales Per Order Method Type);,(Report Sales Per Order Method Type);,(Report Sales Per Order Method Type);,(Report Sales Per Order Method Type);,(Report Sales Per Order Method Type);
...........
INSERT INTO PublishedComments (ReportName) VALUES (Report Sales Per Order Method Type);,(Report Sales Per Order Method Type);,(Report Sales Per Order Method Type);,(Report Sales Per Order Method Type);,(Report Sales Per Order Method Type);,(Report Sales Per Order Method Type);,(Report Sales Per Order Method Type);,(Report Sales Per Order Method Type);,(Report Sales Per Order Method Type);,(Report Sales Per Order Method Type);,(Report Sales Per Order Method Type);,(Report Sales Per Order Method Type);
...........
答案 0 :(得分:0)
两个问题:
首先,您的值不会被repeat
包围。
第二,每个集合都以分号None
结尾,但应为逗号'
第三,在评论中指出,您正在循环使用;
,然后再次循环使用,
...肯定不是您想要的:}
while()
答案 1 :(得分:0)
如果您尝试以整批插入方式插入整行,则可能要从while循环中取出插入查询。 同时删除foreach和“;”在“('.. $ row ['ReportName']。”');“;
while ($row = sqlsrv_fetch_array($getResults,SQLSRV_FETCH_ASSOC))
{
$sql[] = "('".$row['ReportName']."')";
}
$test = "INSERT INTO PublishedComments (ReportName) VALUES ".implode(",",$sql) . ';'; // Add semicolon
echo $test;
echo "<br />...........<br />";