PHP Foreach SQL,INSERT INTO ON DUPLICATE KEY UPDATE只插入最后一个查询?

时间:2016-07-21 10:50:42

标签: php mysql pdo insert-update

PHP,使用PDO更新SQL数据库,只是使用随机数将Warehouse_id更新为phpmyadmin。

function updateWarehouses(){


    global $tvHost, $handler, $apiRequestCall, $sqlError;


    $apiUpdateWarehouses = apiRequestCall("GET", $tvHost, "Product?pageNumber=1");
    $updates = 0;
    foreach($apiUpdateWarehouses['List'] as $c){
        foreach($c['PerWarehouseInventory'] as $i) {

            try {
                    $sql = $handler->prepare("INSERT INTO tv_warehouse (warehouse_id, code) VALUES (:warehouse_id, :code) ON DUPLICATE KEY UPDATE warehouse_id=:warehouse_id, code=:code");
                    echo "<br>".$i['WarehouseID']." ".$i['WarehouseCode'];
                    $sql->execute([
                    ':warehouse_id' => $i['WarehouseID'],
                    ':code' => $i['WarehouseCode']]
                    );
                    $updates++;
                }
            catch(Exception $er) {
                    sqlError($er, $table);
                    die();
            }
            } 
        }
        echo "<br>".$updates." warehouses updated.";
    }

我已打印出您输入的变量,您无法使用echo语句:

运行脚本时在页面上显示(WarehouseID WarehouseCode)

3.5284454726114E+18 Demo
3.5284455793003E+18 Temp Rework
3.5950682694297E+18 TL-Shipments
3.5897270029153E+18 TL-Supplier
3.4684900596836E+18 WH1
5 warehouses updated.

输入的唯一一个是warehouse_id = 2147483647和warehouse_code = WH1

WH1是正确的但是2147483647来自哪里?为什么没有其他人进入?

表格式:

-- -----------------------------------------------------
-- Table `Data`.`tv_warehouse`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `Data`.`tv_warehouse` (
  `code` VARCHAR(45) NOT NULL,
  `warehouse_id` INT(30) NOT NULL,
  PRIMARY KEY (`warehouse_id`))
ENGINE = InnoDB;

JSON:

{
    "List": [
        {

            "PerWarehouseInventory": [
                {
                    "WarehouseID": 3528445472611403000,
                    "WarehouseCode": "Demo",
                },
                {
                    "WarehouseID": 3528445579300286000,
                    "WarehouseCode": "Temp Rework",
                },
                {
                    "WarehouseID": 3595068269429729300,
                    "WarehouseCode": "TL-Shipments",
                },
                {
                    "WarehouseID": 3589727002915314000,
                    "WarehouseCode": "TL-Supplier",
                },
                {
                    "WarehouseID": 3468490059683634700,
                    "WarehouseCode": "WH1",
                }
            ]
        }
    ]
}

任何有关为什么这样做的帮助都很棒我正在使用&#34; json_decode($ data,true);&#34;

如果有帮助,

非常感谢!

1 个答案:

答案 0 :(得分:1)

我找到了对你有用的东西,“2147483647”是Int。

的最大尺寸

尝试使用BigInt或更高级别的东西。

如果你只有一个结果,那是因为你的ID是你的INT,你的ID总是等于2147483647,所以你总是有更新。

这是链接: https://github.com/h5p/h5p-summary/blob/master/js/summary.js#L3