title: PHP foreach() loop then Insert Single Array Value to MySQL DB with Submit Button
body: <p>After json_decode() I run a foreach() loop to display all the json results along with a submit button for each result. I wrote a mysql INSERT block to post the json results. I'm trying to figure out how to assign each json result a unique ID so that after I click the submit button, that JSON object gets posted properly to my database. What is happening currently with my code, is only the last value in the array is getting posted and not the value that I am trying to submit.</p>
  
  <p>I've tried iterating over the foreach() loop and using the iterator as an assigned unique ID. Didn't work. I tried creating checkboxes next to the submit buttons and this did not work out either. I'm not sure if it's my code placement or why I'm only able to post the last value in the array.</p>
  
  <pre><code>&lt;?php
  
  $curl = curl_init();
  
  $provider_first_name = 'Henry';
  $provider_last_name = 'Lee';
  $provider_state = 'CA';
  $provider_postal_code = '';
  
  curl_setopt_array($curl, array(
    CURLOPT_URL =&gt; "https://npiregistry.cms.hhs.gov/api?first_name=". $provider_first_name . "&amp;last_name=" . $provider_last_name . "&amp;state=" . $provider_state . "&amp;postal_code=" . $provider_postal_code . "",
    CURLOPT_RETURNTRANSFER =&gt; true,
    CURLOPT_ENCODING =&gt; "",
    CURLOPT_MAXREDIRS =&gt; 10,
    CURLOPT_TIMEOUT =&gt; 30,
    CURLOPT_HTTP_VERSION =&gt; CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST =&gt; "GET",
  
  ));
  
  $response = curl_exec($curl);
  
  $json = json_decode($response, TRUE);
  </code></pre>
  
  <p>/** 10 Henry Lee Results Will Display from foreach() Loop **/</p>
  
  <pre><code>$i = 0;
  
  foreach($json['results'] as $doc){
  /** JSON Responses Listed Below **/
  
    $p_name = $doc['basic']['first_name'] . " " . $doc['basic']['last_name'] . ", " . $doc['basic']['credential'];
  
    $p_npi = $doc['number'];
  
    $p_address_one = $doc['addresses'][0]['address_1'];
  
    $p_address_two = $doc['addresses'][0]['city'] . ", " . $doc['addresses'][0]['state'] . " " . $doc['addresses'][0]['postal_code'];
  
    $p_skills = $doc['taxonomies'][0]['desc'];
  
    $p_phone = $doc['addresses'][1]['telephone_number'];
  
    if(empty($doc['addresses'][1]['telephone_number'])){
  
      $p_phone = 'Not Available';
    }
  
    //** Echo Each Array Value In Bordered Card **/
  
    echo "
         &lt;div class='col-md-4 col-sm-12 wow fadeIn' style='padding-top: 9px;'&gt;
           &lt;div class='card border border-secondary mb-3' style='max-width: 18rem;'&gt;
             &lt;div class='card-header text-center'&gt;
               &lt;i class='fa fa-user-md' aria-hidden='true'&gt;&lt;/i&gt; $p_name
             &lt;/div&gt;
             &lt;div class='card-body text-primary text-center'&gt;
  
               &lt;p class='card-text text-center'&gt;
                 NPI Number: $p_npi
               &lt;/p&gt;
  
               &lt;p class='card-text text-center'&gt;
                 $i 
               &lt;/p&gt;
  
               &lt;p class='card-text text-center'&gt;
                 &lt;i class='fa fa-map-marker' aria-hidden='true'&gt;&lt;/i&gt; $p_address_one 
               &lt;/p&gt;
  
               &lt;p class='card-text text-center'&gt;
                 $p_address_two 
               &lt;/p&gt;
  
               &lt;p class='card-text text-center'&gt;
                 &lt;i class='fa fa-phone cyan-text' aria-hidden='true'&gt;&lt;/i&gt; $p_phone
               &lt;/p&gt;
  
               &lt;p class='card-text text-center'&gt;
                 &lt;i class='fa fa-user-md cyan-text' aria-hidden='true'&gt;&lt;/i&gt; $p_skills
               &lt;/p&gt;
             &lt;/div&gt;
  
             &lt;div class='card-footer' style='height: 73px;''&gt;
               &lt;form action='' method='post' id='".$json['results'][$i]."'&gt;
                 &lt;div class='form-group'&gt;
                   &lt;div class='flex-center'&gt;
                     &lt;div class='form-row align-items-center'&gt; 
                       &lt;button class='btn btn-pink btn-sm' name='add_doc' type='submit'&gt;&lt;i class='fa fa-trash px-1' aria-hidden='true'&gt;&lt;/i&gt; Add Provider&lt;/button&gt;
                     &lt;/div&gt;
                   &lt;/div&gt;
                 &lt;/div&gt;
               &lt;/form&gt;
             &lt;/div&gt;
           &lt;/div&gt; 
         &lt;/div&gt;
         ";
         $i++;
  
   }
  ?&gt;
  
  &lt;?php
  
    if(isset($_POST['add_doc'])){
  
      $add_doc = "INSERT INTO user_posted_providers (user_id, provider_name, provider_npi, provider_address_one, provider_address_two, provider_phone, provider_skills, created_at) VALUES ('$user_id', '$p_name', '$p_npi', '$p_address_one', '$p_address_two', '$p_phone', '$p_skills', NOW())";
  
      $run_doc = mysqli_query($connection, $add_doc);
  
      if($run_doc){
        echo "&lt;script&gt;alert('Provider Added')&lt;/script&gt;";
        echo "&lt;script&gt;window.open('user_provider_search-r.php','_self')&lt;/script&gt;";
      }
  
    }
  
  ?&gt;
  </code></pre>
  
  <p>I'm trying to insert the proper array values with my 'add_doc' button in the foreach() loop, but I only able to insert the last value in the array.</p>