Kenny Pollock Posted March 12, 2008 Share Posted March 12, 2008 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 More sharing options...
Psycho Posted March 12, 2008 Share Posted March 12, 2008 <?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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.