Currently I am trying to upload a CSV file and enter each record into the database 1 by 1. The columns on the CSV have the same name as the field names in the database but sometimes the data will be in a different order within the CSV. When I say in a different order, I mean that instead of a list of names always being in the 1st column, they might be in the 3rd column.
Really what I'm asking is how will I do the above as I'm really stuck.
At the moment I doesn't insert into the database but it does get the array from the CSV file.
Code Below:
index.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>CSV Import</title>
</head>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="csv"/>
<input type="submit" name="submit" value="Save" />
</form>
</body>
</html>
config.php
<?php
/* Database Connection */
$con = mysql_connect('xxxxxxxx', 'xxxxxxxx', 'xxxxxxxx');
if(! $con )
{
die('Could not connect: ' . mysql_error());
}
$select_db = mysql_select_db('xxxxxxxx');
?>
upload.php
<?php
include('config.php');
$file = "test.csv";
$separator = ",";
$length = filesize($file);
$handle = fopen($file, "r");
$csvData = fgetcsv($handle, $length, $separator);
fclose($handle);
$i = 0;
while($i >= 1){
$title = $csvData[0];
$firstName = $csvData[1];
$secondName = $csvData[2];
$emailAddress = $csvData[3];
$houseNumber = $csvData[4];
$mobileNumber = $csvData[5];
$address1 = $csvData[6];
$address2 = $csvData[7];
$address3 = $csvData[8];
$address4 = $csvData[9];
$postcode = $csvData[10];
mysql_query("INSERT csv SET title='$title', firstName='$firstName' ,secondName='$secondName', emailAddress='$emailAddress', houseNumber='$houseNumber' ,mobileNumber='$mobileNumber', address1='$address1', address2='$address2', address3='$address3' ,address4='$address4', postcode='$postcode'")
$i++;
}
?>