【问题标题】:Insert array into MySQL将数组插入 MySQL
【发布时间】:2013-03-05 22:41:15
【问题描述】:

我非常困惑,一直在寻找。但正如标题所示,我正在尝试输入一个数组。 我的问题是如何让这个数组导入数据库?到目前为止,使用当前脚本,它只导入第一条记录,而不导入其余记录。在这里,我还可以在同一个数组中导入其他值,顺便说一下,这是一个 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')";

【问题讨论】:

  • 我认为您需要在foreach 循环中插入插入查询。

标签: php mysql arrays json


【解决方案1】:

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

Serialize 将其存储在数据库中,unserialize 在您将其从数据库中拉出时使用它

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 我正在考虑使用 PDO 而不是 mysqli 来重做我的代码,我可以插入一个关联数组吗?
    • 不,必须是你。使用 PDO_MySQL 是出于安全原因。您可以有一个准备好的语句,然后只需推送值。
    【解决方案3】:

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

    foreach($array as $value) {
        // execute your insert statement here with $value
    }
    

    【讨论】:

      【解决方案4】:

      首先你不能像你正在做的那样在 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')";
      
                  }
          }
      }
      

      【讨论】:

        【解决方案5】:

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

        <?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)."
        ";
        ?>
        

        【讨论】:

        • 我在不使用下面的内爆或爆炸代码的情况下让它工作!感谢所有的帮助!
        【解决方案6】:

        这是我要解决的问题!

        $stmt = $dbh->准备( “插入到 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();
                    }
                }
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-11-10
          • 2011-04-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-07-15
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多