Mysql批量插入不插入所有数据


Mysql bulk insert not inserting all data

我必须插入许多数据从excel表(约4000行)通过Ajax内循环我使用:

$query = "INSERT IGNORE INTO ".$params["table"]." (".$colonnes_string.") VALUES (".$row.")";

这个查询工作得很好,但它只插入了4000行的1000行,可能是一些插入有错误,为什么我设置IGNORE语句,但它仍然没有插入超过1000行。

如何跳过插入错误并继续查询?

谢谢。

请使用INSERT…ON DUPLICATE KEY UPDATE语法而不是忽略这里有一个类似问题的链接https://softwareengineering.stackexchange.com/questions/238472/how-to-insert-update-a-large-amount-of-data-into-mysql-using-php

您可以使用该语法插入多行:

$query = "
    INSERT INTO some_table 
        (column_name) 
    VALUES 
        ('value'),
        ('another value'),
        ('and again'),
        ...
        ('a last one');
";

因此,您必须同时使用所有VALUESINSERT构建查询。

。:

// Set the first part of the query with table name and column(s)
$query = "INSERT INTO some_table (some_column) VALUES ";
foreach ($rows as $row) {
    // concatenate the query with values in a loop
    $query .= "(".$row."),";
}
// replace last comma with semi-column to get the right syntax
$query = substring($query, 0, -1).";";

希望能有所帮助。