php 一个查询php中的多个mysql INSERT语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1307618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 01:58:01  来源:igfitidea点击:

Multiple mysql INSERT statements in one query php

phpmysql

提问by Ian McCullough

Is this legal?

这是合法的吗?

$string1= "INSERT INTO....;";
$string1 .= "INSERT INTO....;";
$string1 .= "INSERT INTO....;";
mysql_query($string1) or die(mysql_error()); 

回答by Gavin Gilmour

For what it's worth, and depending on if you're inserting the same data into the same tables, it's much better to insert multiple values with the one inserte.g.

对于它的价值,并且取决于您是否将相同的数据插入到相同的表中,最好通过一次插入插入多个值,例如

INSERT INTO a VALUES (1,23),(2,34),(4,33);
INSERT INTO a VALUES (8,26),(6,29);

回答by Greg

No, mysql_query()only allows one query at a time.

不,一次mysql_query()只允许一个查询。

You can insert multiple rows like this:

您可以像这样插入多行:

INSERT INTO table (col1, col2)
VALUES (1, 2), (3, 4), (5, 6)

回答by Matija

From MySQL dev support MySQL dev forum

来自 MySQL 开发支持MySQL 开发论坛

INSERT INTO table (artist, album, track, length) 
VALUES 
("$artist", "$album", "$track1", "$length1"), 
("$artist", "$album", "$track2", "$length2"),
("$artist", "$album", "$track3", "$length3"), 
("$artist", "$album", "$track4", "$length4"),
("$artist", "$album", "$track5", "$length5");

So insert goes as normal as always:

所以插入像往常一样正常:

  • naming first the table name where we want to insert new row,
  • followed by naming column names in round brackets (Note: Not needed if you want to insert ALLcolumns),
  • followed by VALUESkey name and then in round brackets comes the values that you want to insert for new ROW in the above table,
  • followed by COMMAand then another pair of round brackets with new values for new row in the mentioned table above
  • and this repeats N-times as long you have the data to insert.
  • 首先命名我们要插入新行的表名,
  • 然后在圆括号中命名列名(注意:如果要插入所有列,则不需要),
  • 后跟VALUES键名,然后在圆括号中是您要为上表中的新 ROW 插入的值,
  • 后跟逗号,然后是另一对圆括号,上面提到的表格中的新行有新值
  • 只要您有要插入的数据,这就会重复 N 次。

Happy inserting multiple values with ONE insert statement. :)

使用 ONE insert 语句插入多个值很高兴。:)

回答by VoteyDisciple

In general, that's valid SQL since each statement ends with a semicolon, but PHP doesn't allow you to send more than one query at a time, in order to protect against SQL injection attacks that might exploit a poorly written script.

通常,这是有效的 SQL,因为每个语句都以分号结尾,但 PHP 不允许您一次发送多个查询,以防止可能利用编写不良的脚本的 SQL 注入攻击。

You can still use a syntax like:

您仍然可以使用如下语法:

INSERT INTO foo VALUES ('a1', 'b1'), ('a2', 'b2');

回答by Adrian P.

Copy/paste example within a function and a loop (suppose $ids is an array)

在函数和循环中复制/粘贴示例(假设 $ids 是一个数组)

public function duplicateItem($ids)
{
    if (isset($ids[0])){ //at least one item

        $sqlQuery = "INSERT INTO items (item_id, content) VALUES ";

        for($i=0; $i<count($ids); $i++) {

                if ($i == count($ids)-1){
                    $sqlQuery .= "(".$ids[$i][0].", '".$ids[$i][1]."');";
                }else{
                    $sqlQuery .= "(".$ids[$i][0].", '".$ids[$i][1]."'),";
                }

        }

         mysql_query($sqlQuery) or die('Error, insert query failed: '.mysql_error());   

    }   
}

回答by Andrey Jakovenko

 INSERT INTO table (a,b) VALUES (1,2), (2,3), (3,4);

回答by Arun Raj

// if $data is an array       
$sqlQuery ="INSERT INTO tableName(col1, col2) VALUES ";
for($i=0; $i<count($data); $i++) {
     $sqlQuery .="(".$data[$i][0].", '".$data[$i][1]."'),";
}
$sqlQuery = rtrim($sqlQuery, ','); // Remove last comma
mysql_query($sqlQuery) or die('Error, insert query failed: '.mysql_error());

回答by Josh Leitzel

No. They are separate queries, and must be called as such.

不。它们是单独的查询,必须如此调用。