Jump to content

[SOLVED] Multiple table insert from Tab delimited file


gigantorTRON

Recommended Posts

Hello all!

I'm having a hell of a time with an issue here...

 

I have a page I'm coding for batch updates from a tab delimited file. Each row has data that has to be checked against and inserted/updated on different tables in my mySQL database.

 

How would I go about parsing through the tab delimited file and then storing data between the tabs in order to insert individually into separate tables??

 

Here's what I have so far...

 

$contents = file('/myfile.txt');

 

for ($i=0; $i<sizeof($contents); $i++) {

 

$line = trim($contents[$i]);

$arr = explode("\t", $line);

 

$line[$i] = array('ID' => $arr[0], 'Record_Type' => $arr[1], 'First_Name' => $arr[2], 'MI' => $arr[3], 'Last_Name' =>

$arr[4], 'Firm' => $arr[5], 'Address' => $arr[6], 'type' => rtrim($arr[13]));

 

I need to take the different columns.. ID, Record_Type, etc. and update different tables with each value where the IDs match.

 

I'm a bit of a php n00b.

 

Thanks!!

 

Hello there,

 

Try the following script,

<?php
// We get the file as an array here.
$Contents = file('File.txt');

// Loop trough the file using foreach, giving us single lines.
foreach($Contents as $Lines){
list($ID, $Record_Type, $First_Name, $MI, $Last_Name, $Firm, $Address, $Type) = explode('delimiter', $Lines);
$Query = 'UPDATE Data...';
mysql_query($Query);
}
?>

 

Thanks for the reply!

That's about where I've gotten to in the meantime. If I were able to do a straight update it wouldn't be so difficult but I need to compare ID keys first.

 

For example:

if ($myTextArray["id"] = myTable.id)

    then UPDATE query

 

Else (if !($myTextArray["id"] = myTable.id))

  then run INSERT query

 

any more insight?? I'm having trouble comparing the id values. Do I need to do a select query to get all of the id values from mySQL table??

uhm.., Okay..

Try this,

<?php
  $mq = mysql_connect('localhost', 'root', '');
  mysqlselectdb('database', $mq);
  
  $Contents = file('File.txt');
  $Query = mysql_query('SELECT ID FROM Table');
  //Loop trough the table.
  while ($Row = mysql_fetch_array($Query)) {
      // Loop trough the file using foreach, giving us single lines.
      foreach ($Contents as $Lines) {
          list($ID, $Record_Type, $First_Name, $MI, $Last_Name, $Firm, $Address, $Type) = explode('delimiter', $Lines);
          if ($ID == $Row['ID']) {
              $Query = 'UPDATE Data...';
              mysql_query($Query);
          }
      }
  }
?>

Thanks, this definitely helps. However I need the looping to go the "other way." The contents of the read-in file are much smaller than the contents of the table. I need to match the id from the file to the id of the table and not vice versa.

 

Any more help??

oh..

Use this then,

<?php
  $mq = mysql_connect('localhost', 'root', '');
  mysqlselectdb('database', $mq);
  
  $Contents = file('File.txt');
  $Query = mysql_query('SELECT ID FROM Table');
  //Loop trough the table.
  $Row = mysql_fetch_array($Query);
      // Loop trough the file using foreach, giving us single lines.
      foreach ($Contents as $Lines) {
          list($ID, $Record_Type, $First_Name, $MI, $Last_Name, $Firm, $Address, $Type) = explode('delimiter', $Lines);
          if ($ID == $Row['ID']) {
              $Query = 'UPDATE Data...';
              mysql_query($Query);
          }
      }
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.