title: inserting data from nested arrays into mysql using php
body: <p>i have this nested array of json data and i am trying to insert specific data into the MYSQL database. However im getting an error and i simply don't know what is wrong with my code. Sorry still new to php/mysql. Any help is appreciated</p>
  
  <p>Here is the json array:</p>
  
  <pre><code>[
  {
  "title": "â˜… (Blackstar)",
  "artist": "David Bowie",
  "year": "2016",
  "genre": "Jazz",
  "media": [
  {
  "totalDiscs": "1",
  "position": "1",
  "tracks": [
  {
  "title": "â˜… (Blackstar)",
  "number": "1",
  "artists": []
  },
  {
  "title": "'Tis A Pity She Was A Whore",
  "number": "2",
  "artists": []
  },
  {
  "title": "Lazarus",
  "number": "3",
  "artists": []
  },
  {
  "title": "Sue (Or In A Season Of Crime)",
  "number": "4",
  "artists": []
  },
  {
  "title": "Girl Loves Me",
  "number": "5",
  "artists": []
  },
  {
  "title": "Dollar Days",
  "number": "6",
  "artists": []
  },
  {
  "title": "I Can't Give Everything Away",
  "number": "7",
  "artists": []
  }
  ]
  }
  ],
  "score": 1
  }
  ]
  </code></pre>
  
  <p>Here is my code:</p>
  
  <pre><code>$json = json_decode($result, true);
  
  $servername = "localhost";
  $username = "root";
  $password = "";
  $dbname = "4tracks";
  
  // Create connection
  $conn = new mysqli($servername, $username, $password, $dbname);
  // Check connection
  if ($conn-&gt;connect_error) {
      die("Connection failed: " . $conn-&gt;connect_error);
  } else {
      //echo "connected &lt;br/&gt;";
  }   
  
  
  
  $sql = "INSERT INTO tracks (artist_name)
  VALUES ('".$json[0]['artist']."')";
  
      if (array_key_exists('genre',$json[0])){
          $sql = "INSERT INTO tracks (track_genre)
              VALUES ('".$json[0]['genre']."')";
  
      }
  
      foreach($json[0]['media'] as $key =&gt; $values){
  
  
          foreach($values['tracks'] as $key1 =&gt; $values1) {
              $sql .= "INSERT INTO tracks (track_name)
                  VALUES ('".$values1['title']."')";
  
  
          }
  }
  
  
  
  if ($conn-&gt;query($sql) === TRUE) {
      echo "New record created successfully";
  } else {
      echo "Error: " . $sql . "&lt;br&gt;" . $conn-&gt;error;
  }
  
  $conn-&gt;close();
  </code></pre>
  
  <p>Here is the output when i run the .php on wamp:</p>
  
  <blockquote>
    <p>Error: INSERT INTO tracks (artist_name) VALUES ('David Bowie'); INSERT
    INTO tracks (track_genre) VALUES ('Jazz');</p>
    
    <p>INSERT INTO tracks (track_name) VALUES ('★ (Blackstar)');</p>
    
    <p>INSERT INTO tracks (track_name) VALUES (''Tis A Pity She Was A
    Whore');</p>
    
    <p>INSERT INTO tracks (track_name) VALUES ('Lazarus');</p>
    
    <p>INSERT INTO tracks (track_name) VALUES ('Sue (Or In A Season Of
    Crime)');</p>
    
    <p>INSERT INTO tracks (track_name) VALUES ('Girl Loves Me');</p>
    
    <p>INSERT INTO tracks (track_name) VALUES ('Dollar Days');</p>
    
    <p>INSERT INTO tracks (track_name) VALUES ('I Can't Give Everything
    Away');</p>
    
    <p>You have an error in your SQL syntax; check the manual that
    corresponds to your MySQL server version for the right syntax to use
    near 'INSERT INTO tracks (track_genre) VALUES ('Jazz');INSERT INTO
    tracks (track_n' at line 2---</p>
  </blockquote>