//create function to make db safe
function make_safe($string){
$string=strip_tags($string);// strip out tags
if (get_magic_quotes_gpc())// if system setting magic quotes is on
{
$string = stripslashes($string);//strip out escape slashes
}
mysql_real_escape_string($string);// sanitise string
return $string; //produce result
}
$handle = fopen($new_file_path, "r");//open read only connection
$data = fgetcsv($handle);// get data from open file
//start row count
$row=1;
while ($data !== FALSE) {//while there is data..
$fields = count($data);//count data fields in row
for ($i=0; $i < $fields; $i++) { //loop through fields from 0
//define column values and sanitise
$groupcode = $_SESSION['MM_GroupCode'];
$memid = createRandomString();
$username = createRandomString();
$level = "Unvalidated";
$password = createRandomString();
$createdwhen = time();
$createdby = $_SESSION['MM_Username'];
$editedwhen = time();
$editedby = $_SESSION['MM_Username'];
$accno = make_safe($data[0]);
$accman = make_safe($data[1]);
$company = make_safe($data[2]);
$logo = make_safe($data[3]);
$title = make_safe($data[4]);
$firstname = make_safe($data[5]);
$lastname = make_safe($data[6]);
$dear = make_safe($data[7]);
$position = make_safe($data[8]);
$dept = make_safe($data[9]);
$add1 = make_safe($data[10]);
$add2 = make_safe($data[11]);
$add3 = make_safe($data[12]);
$city = make_safe($data[13]);
$county = make_safe($data[14]);
$postcode = make_safe($data[15]);
$country = make_safe($data[16]);
$homeadd1 = make_safe($data[17]);
$homeadd2 = make_safe($data[18]);
$homeadd3 = make_safe($data[19]);
$homecity = make_safe($data[20]);
$homecounty = make_safe($data[21]);
$homepostcode = make_safe($data[22]);
$homecountry = make_safe($data[23]);
$switchboard = make_safe($data[24]);
$directtel = make_safe($data[25]);
$fax = make_safe($data[26]);
$mobile = make_safe($data[27]);
$hometel = make_safe($data[28]);
$email = make_safe($data[29]);
$homeemail = make_safe($data[30]);
$website = make_safe($data[31]);
$umbcomp = make_safe($data[32]);
$subsidiaries = make_safe($data[33]);
$brands = make_safe($data[34]);
$tps = make_safe($data[35]);
$fps = make_safe($data[36]);
$mps = make_safe($data[37]);
$thirdpp = make_safe($data[38]);
$newsp = make_safe($data[39]);
$socgrade = make_safe($data[40]);
$acorndem = make_safe($data[41]);
$usertz = "UTC";
$usertf = "Y-m-d H:i:s";
$info = make_safe($data[42]);
} //close loop
//insert into db
$insert = "INSERT INTO CONTACTS (GROUPCODE, MEMID, USERNAME, LEVEL, PASSWORD, CREATEDWHEN, CREATEDBY, EDITEDWHEN, EDITEDBY, ACCNO, ACCMAN, COMPANY, LOGO, TITLE, FIRSTNAME, LASTNAME, DEAR, POSITION, DEPT, ADD1, ADD2, ADD3, CITY, COUNTY, POSTCODE, COUNTRY, HOMEADD1, HOMEADD2, HOMEADD3, HOMECITY, HOMECOUNTY, HOMEPOSTCODE, HOMECOUNTRY, SWITCHBOARD, DIRECTTEL, FAX, MOBILE, HOMETEL, EMAIL, HOMEEMAIL, WEBSITE, UMBCOMP, SUBSIDIARIES, BRANDS, TPS, FPS, MPS, 3RDPP, NEWSP, SOCGRADE, ACORNDEM, USERTZ, USERTF, INFO)
VALUES ('$groupcode', '$memid', '$username', '$level', '$password', '$createdwhen', '$createdby', '$editedwhen', '$editedby', '$accno', '$accman', '$company', '$logo', '$title', '$firstname', '$lastname', '$dear', '$position', '$dept', '$add1', '$add2', '$add3', '$city', '$county', '$postcode', '$country', '$homeadd1', '$homeadd2', '$homeadd3', '$homecity', '$homecounty', '$homepostcode', '$homecountry', '$switchboard', '$directtel', '$fax', '$mobile', '$hometel', '$email', '$homeemail', '$website', '$umbcomp', '$subsidiaries', '$brands', '$tps', '$fps', '$mps', '$thirdpp', '$newsp', '$socgrade', '$acorndem', '$usertz', '$usertf', '$info');";
mysql_query($insert) OR die(mysql_error());
echo "Record ".$row." successfully imported with ".$fields." fields.<br>";
$row++;// increase row number
$data = fgetcsv($handle);//get data from open file whilst looping
}
fclose($handle);// close open file connection