Importing CSV To MySQL
Posted: Thu Jul 27, 2006 6:00 pm
feyd | Please use
feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
[b]Hey All...[/b]
Fairly new to PHP but learning....
I have a site with a little "members area" and I want to allow people to import some data ([i]a contact list[/i]) into a MYSQL table.
I have this script, see code below, that takes the submitted form, from when the users uploads the photo, and places the info into the table.
It works just fine, The script also grabs the "list id" from the form and the "member id" fomr the sessoin to include this data in the table as well - this part is working fine as well...
But my problem is this:
It only works if the tables, based on the headers of the CVS file, are the exact same as the database, in the exact order...
Now I don't mind making my users have the CVS header field name match the database, but sometimes it not always in the exact order and/or they are missing a field ([i]for example, they don't the feild "fax number"[/i]).
I am wondering if there is any way I could modify the script to make it so it will like "look" at the headers of the CVS file and match tehm up correctly (so long as the names match), so they don't have to be in the exact order ([i]eg: name before email, I would like it to still work, if they had email before name[/i]).
and if there were any fields missing ([i]say they did'nt have "name"[/i]) the script would just skip it and move on to "email" and import that.
[b]Hopefully this makes sence...[/b] Also, the CSV file could be of any size, 5 record, or 50, so we can't limit it, within reason anyway...Code: Select all
function process_import() {
global $Member_ID;
$csvFile = $_FILES['csv_file']['tmp_name'];
if(empty($csvFile)) die('Please select a CSV file to import from');
$extension = explode(".", basename($_FILES['csv_file']['name']));
$extension = strtoupper( $extension[count($extension) -1] );
if($extension == 'CSV' || $extension == 'TXT') {
$fileArray = file($csvFile);
$i = 0;
foreach ($fileArray as $line) {
$lineArray = explode(',', str_replace('"', '', $line));
$fieldArray[$i][0] = $lineArray[1];
$fieldArray[$i][1] = $lineArray[0];
$fieldArray[$i][2] = $lineArray[2];
$fieldArray[$i][3] = $lineArray[3];
$fieldArray[$i][4] = $lineArray[4];
$fieldArray[$i][5] = $lineArray[5];
$fieldArray[$i][6] = $lineArray[6];
$fieldArray[$i][7] = $lineArray[7];
$fieldArray[$i][8] = $lineArray[8];
$i++;
}
$dbConnection = db_connect() or die(sql_error());
$importCount = 0;
for($i = 0; $i < count($fieldArray); $i++) {
$invalid = false;
if(!ereg("^[^@ ]+@[^@ ]+\.[^@ \.]+$", $fieldArray[$i][0]))
$invalid = true;
if(!$invalid){
$sSQL = 'INSERT INTO maillist_users(list_id, user_name, email, address, city, state, zip, phone, phone_other, faxnumber, realtor_id, date_added) ';
$sSQL .= 'VALUES(' . $_POST['list_id'] . ", '" . $fieldArray[$i][0] . "', '";
$sSQL .= $fieldArray[$i][1] . "', '";
$sSQL .= $fieldArray[$i][2] . "', '";
$sSQL .= $fieldArray[$i][3] . "', '";
$sSQL .= $fieldArray[$i][4] . "', '";
$sSQL .= $fieldArray[$i][5] . "', '";
$sSQL .= $fieldArray[$i][6] . "', '";
$sSQL .= $fieldArray[$i][7] . "', '";
$sSQL .= $fieldArray[$i][8] . "', $Member_ID, NOW())";
mysql_query($sSQL) or die(sql_error());
$importCount++;
}
}
print_header();
echo '<h2 align="center">Import Contacts</h2>';
echo '<p align="center">' . $importCount . ' contacts were successfully imported.</p>';
}
else die('The file you specified is not a valid CSV file');
print_footer();
}feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]