Jump to content

Recommended Posts

I have a file with 4000 rows of information. Comma-separated.

 

How would I go about running a script that reads the file, checks that the first 3 values (company/contact/zip code) of the file match a record in the database, and then add the remaining values into that company's row in the database?

 

Every company on the list has a row in the MySQL database already... just missing information needs to be filled in from this text file.

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/95868-import-from-file/
Share on other sites

<?php

//Read import file into an array
$data = file("import_file.txt");

//Iterrate through each record
foreach ($data as $record) {

    //Explode record into individual data elements
    $record_set = explode(',', $record)

    //Create the query
    //modify the field names and record values as needed
    $query = "UPDATE table

              SET fieldA = '{$record[3]}',
                  fieldB = '{$record[4]}',
                  fieldC = '{$record[5]}',
                  fieldD = '{$record[6]}'

              WHERE company = '{$record[0]}'
                AND contact = '{$record[1]}'
                AND zip = '{$record[2]}'"

    //Update the records (if exist)
    mysql_query($query) or die (mysql_error());
}

?>

Link to comment
https://forums.phpfreaks.com/topic/95868-import-from-file/#findComment-490829
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.