Hi,
I am trying to insert the values of a csv file into a table with the following code:
public function InsertCSVFileToDB(){
$has_title_row = true;
$not_done = array();
if(is_uploaded_file($_FILES['csvfile']['tmp_name'])){
$filename = basename($_FILES['csvfile']['name']);
if(substr($filename, -3) == 'csv'){
$tmpfile = $_FILES['csvfile']['tmp_name'];
if (($fh = fopen($tmpfile, "r")) !== FALSE) {
$i = 0;
while (($items = fgetcsv($fh, 10000, ",")) !== FALSE) {
if($has_title_row === true && $i == 0){ // skip the first row if there is a tile row in CSV file
$i++;
continue;
}
$sql = "INSERT INTO ConfPaper SET
CPRid = '{$items[0]}',
Pid = '{$items[1]}',
CPtitle = '".mysql_real_escape_string($items[3])."',
CPabstract = '".mysql_real_escape_string($items[4])."',
CPspage = ".mysql_real_escape_string($items[5]).",
CPepage = ".mysql_real_escape_string($items[6]).",
CPlastesited = now()";
if(!mysql_query($sql)){
$not_done[] = $items;
}
$i++;
}
}
// if there are any not done records found:
if(!empty($not_done)){
echo "<strong>There are some records could not be inserted</strong><br />";
print_r($not_done);
}
}
else{
die('Invalid file format uploaded. Please upload CSV.');
}
}
else{
die('Please upload a CSV file.');
}
}
When i try yo upload i get this:
There are some records could not be inserted
Array ( [0] => Array ( [0] => 9 [1] => 1 [2] => CSV1 [3] => 4 [4] => 4 [5] => 01625 584412 ) [1] => Array ( [0] => 9 [1] => 1 [2] => CSV2 [3] => 14 [4] => 24 [5] => 01625 584412 ) )
Any ideas why this is?
This is my CSV file:
http://www.prima.cse.salford.ac.uk:8080/~ibrarhussain/ConfPaper.csv
Can anyone help?