大家好,所以我可以从 json url 更新记录并将其放入 Mysql 现在我正在尝试更新记录,它只获取 1 个值并将所有记录更新为新值,因此它只更新现有记录这里有 1 条记录是我正在使用的
$ApiLink = "JSON URL HERE";
$json_decode = (json_decode(file_get_contents($ApiLink), true));
$output = $json_decode["character_list"];
/************************************************/
$do_stuff = $dbh->prepare("UPDATE damage_given_vehicle SET veh_id=:veh_id, veh_name=:veh_name, veh_total=:total_value, veh_faction_nc=:veh_faction_nc, veh_faction_tr=:veh_faction_tr, veh_faction_vs=:veh_faction_vs WHERE character_number = :char_id");
foreach ($output as $key => $value) {
$character_id[$key] = $output[$key]["id"];
if (isset($output[$key]["stats"]["vehicle_kills"]["vehicle"])) {
$vehicle_kills[$key] = $output[$key]["stats"]["vehicle_kills"]["vehicle"];
foreach ($vehicle_kills[$key] as $row) {
$do_stuff->bindValue(':char_id', $update_id);
$do_stuff->bindValue(':veh_id', $row["id"]);
$do_stuff->bindValue(':veh_name', $row["name"]);
$do_stuff->bindValue(':total_value', $row["value"]);
$do_stuff->bindValue(':veh_faction_nc', $row["faction"]["nc"]);
$do_stuff->bindValue(':veh_faction_tr', $row["faction"]["tr"]);
$do_stuff->bindValue(':veh_faction_vs', $row["faction"]["vs"]);
$do_stuff->execute();
}
}
}
数据库起初看起来像这样:
key | id | name | value 1 | value 2 | value 3 | value 4 |
5428029729515051201 11 name_1 6 45 23 34
5428029729515051201 11 name_2 2 34 63 63
5428029729515051201 11 name_3 34 0 17 17
在我使用更新脚本后,它会将这些值转换为:
5428029729515051201 11 name 34 0 17 17
5428029729515051201 11 name 34 0 17 17
5428029729515051201 11 name 34 0 17 17
如果您对脚本如何在这里输入 json 数据感到好奇,请执行以下操作:
$stmt = $dbh->prepare("
INSERT INTO damage_given_vehicle (
character_number, veh_id, veh_name, veh_total,
veh_faction_nc, veh_faction_tr, veh_faction_vs)
VALUES(
:char_id, :veh_id, :veh_name, :total_value,
:veh_faction_nc, :veh_faction_tr, :veh_faction_vs)
");
foreach ($output as $key => $value) {
$character_id[$key] = $output[$key]["id"];
if (isset($output[$key]["stats"]["vehicle_kills"]["vehicle"])) {
$vehicle_kills[$key] = $output[$key]["stats"]["vehicle_kills"]["vehicle"];
foreach ($vehicle_kills[$key] as $row) {
$stmt->bindValue(':char_id', $character_id[$key]);
$stmt->bindValue(':veh_id', $row["id"]);
$stmt->bindValue(':veh_name', $row["name"]);
$stmt->bindValue(':total_value', $row["value"]);
$stmt->bindValue(':veh_faction_nc', $row["faction"]["nc"]);
$stmt->bindValue(':veh_faction_tr', $row["faction"]["tr"]);
$stmt->bindValue(':veh_faction_vs', $row["faction"]["vs"]);
$stmt->execute();
}
}
}
我如何让它分别更新每个数组,如果我在任何 $row[""] 上执行 var_dump,它们会生成数组应该如何显示,但是当它通过并进入 mysql 时,它只获取 1 个值
谢谢!