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!!

 

Link to comment
Share on other sites

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);
}
?>

 

Link to comment
Share on other sites

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??

Link to comment
Share on other sites

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);
          }
      }
  }
?>

Link to comment
Share on other sites

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);
          }
      }
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.