After searching in vain for a complete script to import csv to mySQL, I created this code to upload your outlook contacts to an online party planner. This code includes the form as well as the code to process the data and insert it into the database.
Hopefully it will save someone a little effort.
Al you have to do is tweak the database values to fit your environment.
The address list id is delivered via the querystring like:
this_page.php?QpartyID=43
The script/checkOutlookForm.js file should look like this:
function checkOutlook(form) {
if (form.csvfile.value == "")
{
alert("Please Select your CSV file.");
form.csvfile.focus();
return (false);
}
}
I need to make this more robust. right now it only tests if there is something selected in the file browser.
you can see it in action here: http://www.groovything.com/default.php?page=viewParty&menu=p_p
<script language="javascript" src="script/checkOutlookForm.js"></script>
<table cellpadding="10" border="0"><tr><td>
<p class="header">Upload your Outlook Contacts</p>
<? if($Qsubmit=="y"){?>
<table width="100%" cellpadding="1" cellspacing="0" border="0"><tr><td class="menuBG"><table width="100%" bgcolor="ffffcc" cellpadding="10" cellspacing="0" border="0"><tr><td>
<center><b><font color="#FF0000">Your Contacts have been added to your party.</font></b></center>
</td></tr></table></td></tr></table>
<?}else{?>
<b>The First step is to export your Outlook Contacts to a <font color="#FF0000">CSV</font> file:</b>
<p>In Outlook, on the <b>File</b> menu, click <b>Import and Export</b>.<br>
Click <b>Export to a file</b>, and then click <b>Next</b>.<br>
Click <b>Comma Separated Values (Windows)</b>, and then click <b>Next</b>.<br>
In the folder list, click the <b>Contacts</b> folder, and then click <b>Next</b>.<br>
<b>Browse</b> to the folder where you want to save the contacts as a .csv file.<br>
Type a name for the exported file, and then click <b>OK</b>.<br>
Click <b>Next</b>.<br>
Click <b>Finish</b>.<br>
<p>When you have completed the steps above, <b>browse</b> to the csv file you have just created and then click <b>Upload</b></p>
<b><font color="#FF0000">CSV files only</font></b><form name="form" action="default.php?page=loadOutlook&QpartyID=<? echo $QpartyID ?>&Qsubmit=y" enctype="multipart/form-data" method="POST">
<table width="100%" cellpadding="1" cellspacing="0" border="0"><tr><td class="menuBG"><table width="100%" bgcolor="ffffcc" cellpadding="10" cellspacing="0" border="0"><tr><td>
<input name="STRpartyID" type="hidden" value="<? echo $QpartyID ?>">
<input name="csvfile" type="file" /></td><td>
<input type="submit" value="Upload" onClick="return(checkOutlook(form))"/></td></tr></table></td></tr></table>
</form>
<?}?>
<?php
csv2DB('csvfile',$STRpartyID);
function csv2DB($filename, $STRpartyID)
{
if (FALSE == empty($_FILES[$filename]['tmp_name']))
{
$row = 1;
$handle = fopen($_FILES[$filename]['tmp_name'], "r");
while (($data = fgetcsv($handle, 1000, ","))!== FALSE)
{
$num = count($data);
if($row == 1){
for($c=1; $c < $num+1; $c++)
{
$invitee_labels[$data[$c]] = $data[$c];
}
$count = 1;
foreach ($invitee_labels as $key => $value )
{
if($key =="First Name"){
$count1 = $count;
}
if($key =="Last Name"){
$count2 = $count;
}
if($key =="E-mail Address"){
$count3 = $count;
}
$count += 1;
}
//echo $count1.",".$count2.",".$count3;
$row++;
}else{
$row++;
$invitee_data[$data[$count1]] = $data[1];
$invitee_data[$data[$count2]] = $data[2];
$invitee_data[$data[$count3]] = $data[3];
DB_insert($STRpartyID,$invitee_data);
$invitee_data = "";
}}
fclose($handle);
}
}
function DB_insert($partyID,$data)
{
// DB function used:
$connection = mysql_connect("localhost","[B][COLOR=red]username[/COLOR][/B]","[B][COLOR=red]password[/COLOR][/B]")
or die ("Couldn't connect to server.");
//select db
$db = mysql_select_db("[B][COLOR=red]database[/COLOR][/B]", $connection) or die("Couldn't select database.");
$count = 1;
$query = "INSERT into [B][COLOR=red]table_name[/COLOR][/B]([B][COLOR=red]contact_list_id[/COLOR][/B], [B][COLOR=red]contact_name[/COLOR][/B], [B][COLOR=red]Email[/COLOR][/B])VALUES ('";
$query .= $partyID;
$query .= "', ";
foreach ($data as $key => $value )
{
if($count == 1)
{
$query .= "'".$key." ";
}
if($count == 2)
{
$query .= $key."', ";
}
if($count == 3)
{
$query .= "'".$key."'";
}
$count += 1;
}
$query .= ");";
$query = str_replace(", );", ");", $query);
//echo $query."<br>";
return mysql_query($query)or die (mysql_error());
$query = "";
}
?>
<p><a href="default.php?menu=p_p&page=viewParty&QpartyID=<? echo $QpartyID ?>">Click here to return to your party</a></p>
</td></tr></table>