Jump to content

Import from file?


Kenny Pollock

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

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.