1

我超级困惑,一直在寻找。但正如标题所示,我正在尝试输入一个数组。我的问题是如何让这个数组导入数据库?到目前为止,使用当前脚本,它只导入第一条记录,而不导入其余记录。在这里,我还可以在同一个数组中导入其他值,顺便说一下,这是一个 JSON 调用,它已经被解码。

foreach ($output as $key => $value) {
    if (isset($output[$key]["stats"]["damage_given"]["vehicle"])) {
        $damage_given[$key] = $output[$key]["stats"]["damage_given"]["vehicle"];
            foreach ($damage_given[$key] as $vehicle_name) {
                $vehicle_dmg_id         = $vehicle_name['id'];
                $vehicle_dmg_name       = $vehicle_name['name'];
                $vehicle_dmg_value      = $vehicle_name['value'];
                $vehicle_dmg_faction_nc = $vehicle_name['faction']['nc'];
                $vehicle_dmg_faction_tr = $vehicle_name['faction']['tr'];
                $vehicle_dmg_faction_vs = $vehicle_name['faction']['vs'];
            }
     }
}

$add_dmg_veh = "INSERT INTO damage_given(character_number, vehicle_id,
vehicle_name, total_value, vehicle_faction_nc, vehicle_faction_tr,
vehicle_faction_vs) VALUES ('$character_id[$key]', '$vehicle_dmg_id', 
'$vehicle_dmg_name','$vehicle_dmg_value', '$vehicle_dmg_faction_nc', 
'$vehicle_dmg_faction_tr','$vehicle_dmg_faction_vs')";
4

6 回答 6

1

尽管不建议将数组存储在数据库中,但您可以serialize()将数组存储在数据库中。基本上,PHP 会将数组转换为特制的字符串,以便稍后对其进行解释。

序列化以将其存储在数据库中,并在将其从数据库中拉出时对其进行反序列化以使用

注意:我说不建议使用序列化,因为您的数据库不是First Normal Form,特别是因为您将非原子值存储在数据库的特定条目中。对于这种情况,我建议创建一个单独的表来单独存储这些值,并使用外键将两个表链接在一起。

于 2013-03-17T15:35:02.167 回答
0

您应该查看 PDO_MySQL 并且您的插入字符串在循环之外,应该在其中执行。

于 2013-03-17T15:35:08.303 回答
0

您必须遍历数组并按自己的方式插入数组的每个字段。

foreach($array as $value) {
    // execute your insert statement here with $value
}
于 2013-03-17T15:35:15.583 回答
0

首先,你不能像你正在做的那样在 MySQL 中插入数组.. 和迭代一样..

foreach ($output as $key => $value) {
    if (isset($output[$key]["stats"]["damage_given"]["vehicle"])) {
        $damage_given[$key] = $output[$key]["stats"]["damage_given"]["vehicle"];
            foreach ($damage_given[$key] as $vehicle_name) {
                $vehicle_dmg_id         = $vehicle_name['id'];
                $vehicle_dmg_name       = $vehicle_name['name'];
                $vehicle_dmg_value      = $vehicle_name['value'];
                $vehicle_dmg_faction_nc = $vehicle_name['faction']['nc'];
                $vehicle_dmg_faction_tr = $vehicle_name['faction']['tr'];
                $vehicle_dmg_faction_vs = $vehicle_name['faction']['vs'];


                // if you wants to use insert query then do here.
                $add_dmg_veh = "INSERT INTO damage_given(character_number, vehicle_id, 
                vehicle_name, total_value, vehicle_faction_nc, vehicle_faction_tr, 
                vehicle_faction_vs) VALUES ('$character_id[$key]', '$vehicle_dmg_id',
                '$vehicle_dmg_name', '$vehicle_dmg_value', '$vehicle_dmg_faction_nc',
                '$vehicle_dmg_faction_tr', '$vehicle_dmg_faction_vs')";

            }
    }
}
于 2013-03-17T15:37:31.263 回答
0

尝试在数组中构建插入数据,然后将结果内爆到单个查询中:

<?php
foreach ($output as $key => $value) {
    if (isset($output[$key]["stats"]["damage_given"]["vehicle"])) {
        $damage_given[$key] = $output[$key]["stats"]["damage_given"]["vehicle"];
            foreach ($damage_given[$key] as $vehicle_name) {

                $sql[] = "
                    (
                    ".$vehicle_name['id'].",
                    ".$vehicle_name['name'].",
                    ".$vehicle_name['value'].",
                    ".$vehicle_name['faction']['nc'].",
                    ".$vehicle_name['faction']['tr'].",
                    ".$vehicle_name['faction']['vs']."
                    )";
            }
    }
}

$query = "
INSERT INTO damage_given
(
character_number,
vehicle_id, 
vehicle_name, 
total_value, 
vehicle_faction_nc, 
vehicle_faction_tr, 
vehicle_faction_vs
) 
VALUES
".implode(",",$sql)."
";
?>
于 2013-03-17T15:49:17.703 回答
0

这就是我要解决的问题!

$stmt = $dbh->prepare("INSERT INTO kills_vehicle (character_number, veh_id, veh_name, veh_total, veh_faction_nc, veh_faction_tr, veh_faction_vs) VALUES(:char_id, :id, :vehname, :total_value, :faction_nc, :faction_tr, :faction_vs )");

    foreach ($output as $key => $value) {
        if (isset($output[$key]["stats"]["play_time"]["vehicle"])) {
            $character_id[$key]    = $output[$key]["id"];
            $score_hit_count[$key] = $output[$key]["stats"]["kills"]["vehicle"];
            foreach ($score_hit_count[$key] as $row) {

                $stmt->bindValue(':char_id', $character_id[$key]);
                $stmt->bindValue(':id', $row[id]);
                $stmt->bindValue(':vehname', $row[name]);
                $stmt->bindValue(':total_value', $row[value]);
                $stmt->bindValue(':faction_nc', $row[faction][nc]);
                $stmt->bindValue(':faction_tr', $row[faction][tr]);
                $stmt->bindValue(':faction_vs', $row[faction][vs]);
                $stmt->execute();
            }
        }
    }
于 2013-03-18T16:07:06.380 回答