使用MySQL PHP更新批量项目

时间:2016-07-06 15:13:46

标签: php mysql bulkupdate

我正在尝试使用一个批量查询更新我的数据库表中的3,000多条记录,但是我遇到了带有我的id数组的路障。想知道我在这里做错了什么?

以下是我收到的错误:

  

检查与您的MySQL服务器版本对应的手册,以便在'附近使用正确的语法')END WHERE InventoryID IN(recs8AFaXZWvAlDIP,recvwDSutfQ66Jtj8,recQ1LODI7ED1FRT'

以下代码:

$sql_connect = mysqli_connect("blah", "blah", "blah", "blah");

$sql = "SELECT inventory.MCategoryID, inventory.InventoryID, inventory.SCategoryID, inventory.ManufacturerID, maincategory.CategoryName as mainCategoryNameMatch, subcategory.SubCategoryName as subCategoryNameMatch, manufacturer.ManufacturerName as manufacturerNameMatch FROM inventory LEFT JOIN maincategory ON inventory.MCategoryID = maincategory.AirTableID LEFT JOIN subcategory ON inventory.SCategoryID = subcategory.AirTableSubCategoryID LEFT JOIN manufacturer ON inventory.ManufacturerID = manufacturer.AirTableManufacturerID ORDER BY CambridgeID";
$doit = mysqli_query($sql_connect, $sql) or die(mysqli_error($sql_connect));
$updateSQL = '';
$uniqueIDArray = [];
$bulkUpdate = '';
$bulkID = '';
while($results = mysqli_fetch_object($doit)){
    $mainCategoryNameResult = $results->mainCategoryNameMatch;
    $subCategoryNameResult = $results->subCategoryNameMatch;
    $manufacturerNameResult = $results->manufacturerNameMatch;
    $uniqueID = $results->InventoryID;      
    $uniqueIDArray[] = $uniqueID;


    $updateSQL .= "WHEN '$uniqueID' THEN '$mainCategoryNameResult'\n";


}
//echo $updateSQL;
$id_list = implode(',', $uniqueIDArray);    
echo "UPDATE inventory SET MainCategoryNameMatch = (CASE InventoryID $updateSQL END) WHERE InventoryID IN ($id_list)<br/>";
if (mysqli_query($sql_connect, "UPDATE inventory SET MainCategoryNameMatch = (CASE InventoryID $updateSQL) WHERE InventoryID IN ($id_list)") or die(mysqli_error($sql_connect))){
    echo $updateSQLStatement;
}   

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

ID是字符串 - 它们必须用引号括起来:

$id_list = '"' . implode('","', $uniqueIDArray) . '"';