title: Check to see if file changes then insert into database
body: <p>Im working on a project that will collect data from XML files and write them to a database. I have a script set up that will be run via a cron job to go though the list of XML files and write the ones that have a newer date to the database so that I have a record of the xml files. I have the timestamp of the last insert for each record in the database. However I'm running into an issue with my logic when trying to accomplish my goal. Any assistance would be great. Im getting an error that says Notice: Trying to access array offset on value of type null in aeiArchive.php on line 37 Ill denote line 37 in my code.</p>
  <pre><code>$readers= glob(&quot;AEI/*xml&quot;);
  
  if (is_array($readers)) {
      foreach ($readers as $reader) {
  //This will have to be edidted to look at more than one file. 
  $xml = simplexml_load_file($reader) or die(&quot;Error: Cannot create object&quot;);
  $timestamp = date(&quot;m/d/y H:i:s&quot;, filemtime($reader));
  //Foreach Loop for the AEI Report from the AEI reader
  foreach ($xml-&gt;xpath(&quot;//AEI_Report&quot;) as $aei_info){
      $totalaxles=(string)$aei_info-&gt;totalaxles;
      $totaltons=(string)$aei_info-&gt;totaltons;
      $trainspeedmph=(string)$aei_info-&gt;trainspeedmph;
      $totalloads=(string)$aei_info-&gt;totalloads;
      $totalmtys=(string)$aei_info-&gt;totalmtys;
      $trainsymbol=(string)$aei_info-&gt;trainsymbol;
      $scannername=(string)$aei_info-&gt;scannername;
      //$reportdata=(string)$aei_info-&gt;repoartdata;
      $sqlf = &quot;SELECT insert_time FROM aei_log WHERE aei_scanner_name = ?&quot;;
      $stmtf = $conn-&gt;prepare($sqlf);
      $stmtf-&gt;bind_param(&quot;s&quot;, $scannername);
      $stmtf-&gt;execute();
      $resultf = $stmtf-&gt;get_result();
      while ($row = $resultf-&gt;fetch_assoc()) {
          $row['insert_time'];
      }
      if ($timestamp &gt;= $row['insert_time']) { //Line 37
      die();
  }else{
      $sql = &quot;INSERT INTO aei_log (aei_scanner_name, aei_train_symbol, aei_train_speed, aei_axles, aei_lds, aei_mtys, aei_tons, insert_time) VALUES (?,?,?,?,?,?,?,?)&quot;;
          $stmt = $conn-&gt;prepare($sql);
          //echo $conn-&gt;error;
          $stmt-&gt;bind_param(&quot;ssssssss&quot;, $scannername, $trainsymbol, $trainspeedmph, $totalaxles, $totalloads, $totalmtys, $totaltons, $timestamp);
          $stmt-&gt;execute();
          $last_id = $stmt-&gt;insert_id;
          $result = $stmt-&gt;get_result();
          if (isset($result)){
              $affectedRow ++;
          }else{
              $error_message = mysqli_error($conn) . &quot;\n&quot;;
          }
  
  }
  
  //Foreach Loop for the train data from the AEI reader
  foreach ($xml-&gt;xpath(&quot;//AEI_Report_UnitData&quot;) as $traininfo){
      $roadname=(string)$traininfo-&gt;roadname;
      $unitnumber=(int)$traininfo-&gt;unitnumber;
      $equipmentype=(string)$traininfo-&gt;equipmentype;
      $isloaded=(string)$traininfo-&gt;isloaded;     
      
      $sql2 = &quot;INSERT INTO aei_unit_data (aei_uid, aei_tr_eqpt, /*aei_tr_dir, aei_tr_seq,*/ aei_tr_rdnm, aei_tr_untnum, aei_tr_lded/*, aei_tr_dest*/) VALUES (?,?,?,?,?)&quot;;
          $stmt = $conn-&gt;prepare($sql2);
          echo $conn-&gt;error;
          $stmt-&gt;bind_param(&quot;issss&quot;, $last_id, $equipmentype, $roadname, $unitnumber, $isloaded);
          $stmt-&gt;execute();
          $result = $stmt-&gt;get_result();
          if (isset($result)){
              $affectedRow ++;
          }else{
              $error_message = mysqli_error($conn) . &quot;\n&quot;;
          }
          
      }
  
  }
  }
  }
  $stmt-&gt;close();
  $conn-&gt;close();
  ?&gt;
  </code></pre>