1

I am super confused and have been searching. But as the title suggests I am trying to enter an array. My question is how do I get this array to import into the database? As of now with the current script, it only imports the first record and not the rest. Here also, I am able to import other values within the same array this is a JSON call by the way and its already being decoded.

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')";
1
  • 1
    I think you need to insert the insert query within the foreach loop. Commented Mar 17, 2013 at 15:33

6 Answers 6

1

Although it is not recommended to store an array in a database, you could serialize() your array to store it in a database. Basically, PHP will convert the array into a specially crafted string, which it can later interpret.

Serialize to store it in the database, and unserialize it to work with it when you pull it out of the database

Note: I say serialization is not recommended, because your database is then not in First Normal Form, specifically because you are storing non-atomic values inside of a particular entry in the database. For this case, I would recommend creating a separate table which can store these values individually, and link the two tables together with a foreign key.

Sign up to request clarification or add additional context in comments.

Comments

0

You should be looking about PDO_MySQL and your insert string is outside the loop and should be execute inside it.

2 Comments

I was looking at redoing my code using PDO instead of mysqli would I be able to insert an associative array with it ?
No, that will have to be you. Using PDO_MySQL is for safety reasons. You can have a prepared statement and then just push the values.
0

You have to iterate through the array and insert every field of the array by it's own.

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

Comments

0

First of all you can't insert array in MySQL as you are doing .. Do as with iterating..

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')";

            }
    }
}

Comments

0

try building your insert data in an array and then implode the results into a single query:

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

1 Comment

I got it to work without using implode or explode Code below! and thanks for all the help!
0

here is what I got to fix the problem!

$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();
            }
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.