HTML5 Multiple File Upload. Return lastinsertid through jQuery AJAX and PHP? : <p>I have an html multiple file upload:</p>

<pre><code>&lt;input type="file" multiple="multiple"&gt;
</code></pre>

<p>I'm successfully uploading files, and writing to my db table:</p>

<p><strong>jQuery</strong></p>

<pre><code>function uploadFile() {

  //some other stuff done here...

  var ajaxRequest =
    $.ajax({
      url: '../inc/ajax-upload.php',
      type: 'POST',
      cache: false,
      processData: false, // important
      contentType: false, // important
      data: formData,
    });
    ajaxRequest.done(function(data) { 

      console.log(data); 
      lastid = 'this comes from data';
      $.each(files, function(i, file) {
        $('div#files').append('&lt;div class="file-name" id="'+ file['name'] +'"&gt;'+file['name']+'&lt;button class="file-delete" id="' + lastid +'"&gt;delete&lt;/button&gt;');
      });

    })
    ajaxRequest.fail(function(jqXHR) { console.log(jqXHR); }) 
}
</code></pre>

<p><strong>php</strong></p>

<pre><code>//CRUD stuff in another file
function pdoInsert($sql, $args) {
    $pdo = dbConnect();
    $stmt = $pdo-&gt;prepare($sql);
    $stmt-&gt;execute($args);
    echo $pdo-&gt;lastInsertId();
}
function addFile($filename){
    $sql = 'INSERT INTO tbl_files (filename) VALUES (?)';
    $args = array($filename);
    echo pdoInsert($sql, $args);
}


//ajax.php file
$lastid = array();
foreach($_FILES as $file) {

    //do filesystem stuff

    //insert to db, get last insert id
    $lastid[] = addFile($filename);  
}
echo json_encode($lastid);
</code></pre>

<p>I want the <code>$lastid</code> from my PHP to be in JSON format like, however, it's returning a concatenated string of the insert id's, e.g. <code>123124[null,null]</code> or something like that. What could be wrong here?</p>
