Hey everybody,
I am stuck working with a csv file that needs to be inserted into a mysql database. This one is driving me crazy. Can anybody tell me where I am messing up here. Maybe my CSV file has some errors in it or my script is wrong. Here is what I am trying to use:
<?php
// connect to DB
require_once('Datafile.php');
mysql_select_db($database_database, $database);
//Move through a CSV file, and output an associative array for each line
ini_set("auto_detect_line_endings", 1);
$current_row = 1;
$matches = glob('*.csv');
$name = basename($matches[0]);
$handle = fopen($name, "r");
while ( ($data = fgetcsv($handle, 10000, ",") ) !== FALSE )
{
$number_of_fields = count($data);
if ($current_row == 1)
{
//Header line
for ($c=0; $c < $number_of_fields; $c++)
{
$header_array[$c] = $data[$c];
}
}
else
{
//Data line
for ($c=0; $c < $number_of_fields; $c++)
{
$data_array[$header_array[$c]] = $data[$c];
}
// Sort through thte data and clean up the data.
//Code removed to shorten the script.
// Loop through the csv file and enter the data into the MySql database.
$insertSQL = sprintf("INSERT INTO data_sheet (one, two, three, four, five, six, seven, eight, nine, ten, eleven, twelve, thirteen, fourteen, fifteen, sixteen, seventeen, eighteen, nineteen, twenty, twentyone) VALUES ($dataOne, $dataTwo, $dataThree, $dataFour, $dataFive, $dataSix, $dataSeven, $dataEight, $dataNine, $dataTen, $dataEleven, $dataTwelve, $dataThirteen, $dataFourteen, $dataFifteen, $dataSixteen, $dataSeventeen, $dataEighteen, $dataNineteen, $dataTwenty, $dataTwentyOne)");
mysql_select_db($database_database, $database);
$Result1 = mysql_query($insertSQL, $database) or die(mysql_error());
}
$current_row++;
}
fclose($handle);
?>
When I run the code I get the following error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near.........
I am cleaning up the data making sure to remove all single quotes, ticks and other bad items. When I look at the csv data, it doesnt look like it should be doing this. PLEASE HELP - ANYTHING HELPS