Hi all
I am building a script that uploads a CSV file and I need to insert the content into a table. Seems simple!
The help i need is that I need to obtain the 'name' of the columns and use them to ensure I insert the correct column data into the correct corresponding database field.
The CSV files will always contain four columns:
Firstname
Lastname
Dob
Col
However the order of the columns may be different each time a CSV is uploaded so i can't just assume the Lastname column will be the second column of the CSV.
How can I parse the CSV file into my database using the names of the columns and if the names of the columns do not match as above, make the import fail?
Many thanks for reading.
Here is what I have so far:
$fcontents = file('documents/csvs/' . $file_name);
for($i=0; $i<sizeof($fcontents); $i++) {
$line = trim($fcontents[$i]);
$arr = explode("\t", $line);
$sql = "insert into mytable values ('". implode("','", $arr) ."')";
mysql_query($sql);
echo $sql ."<br>\n";
if(mysql_error()) {
echo mysql_error() ."<br>\n";
}
}
kbc1