Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将数组插入MySQL_Php_Mysql_Arrays_Json - Fatal编程技术网

Php 将数组插入MySQL

Php 将数组插入MySQL,php,mysql,arrays,json,Php,Mysql,Arrays,Json,我非常困惑,一直在寻找。但正如标题所示,我正试图进入一个数组。 我的问题是如何将此数组导入数据库?到目前为止,对于当前脚本,它只导入第一条记录,而不导入其余记录。在这里,我还可以在同一个数组中导入其他值。顺便说一下,这是一个JSON调用,它已经被解码了 foreach ($output as $key => $value) { if (isset($output[$key]["stats"]["damage_given"]["vehicle"])) { $damag

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

虽然不建议将数组存储在数据库中,但可以
序列化()
将数组存储在数据库中。基本上,PHP将把数组转换成一个精心编制的字符串,以后可以对其进行解释

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


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

虽然不建议将数组存储在数据库中,但可以
序列化()
将数组存储在数据库中。基本上,PHP将把数组转换成一个精心编制的字符串,以后可以对其进行解释

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


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

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

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

您必须迭代数组,并自己插入数组的每个字段

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

您必须遍历数组,并按其自身插入数组的每个字段

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

首先,您不能像现在这样在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')";

            }
    }
}

首先,您不能像现在这样在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')";

            }
    }
}

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

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

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

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

以下是我解决问题的方法

$stmt=$dbh->prepare( “插入车辆(字符号、车辆id、车辆名称、车辆总数、车辆派系nc、车辆派系tr、车辆派系vs) 值(:char_id,:id,:vehname,:total_value,:partition_nc,:partition_tr,:partition_vs)”


下面是我要解决的问题

$stmt=$dbh->prepare( “插入车辆(字符号、车辆id、车辆名称、车辆总数、车辆派系nc、车辆派系tr、车辆派系vs) 值(:char_id,:id,:vehname,:total_value,:partition_nc,:partition_tr,:partition_vs)”


我认为您需要在
foreach
循环中插入插入查询。我认为您需要在
foreach
循环中插入插入查询。我正在考虑使用PDO而不是mysqli来重新编写代码。我是否能够插入关联数组?不,必须是您。使用PDO_MySQL是出于安全考虑。你可以先准备一条语句,然后再推送这些值。我正在考虑用PDO而不是mysqli来重做我的代码。我能用它插入一个关联数组吗?不,那必须是你。使用PDO_MySQL是出于安全考虑。你可以有一个准备好的语句,然后只推送这些值。我让它在不使用下面的内爆或分解代码的情况下工作!谢谢你的帮助!我没有使用下面的内爆或爆炸代码就让它工作了!谢谢你的帮助!